QBASIC – Find the square of a number

Below is the program to calculate the square of the user input number.

' Program to input a number and display its square
CLS
  INPUT "Enter a number";a
  square = a ^ 2
  PRINT "The square of the number is ";square
END

Explanation:

To find the square, we will be using the exponentiation operator (^). The operator is used to raise a number to an exponent of a designated number. Coming to the example, a ^ 2 is used which means a is raised to the power of 2 giving back the square of a (user input number).

Similarly, to calculate the square, square = a * a can be used instead of square = a ^ 2.

Leave a Comment

Your email address will not be published. Required fields are marked *