Python Program - Square Root of a Number


Advertisements

If a number is multiplied by itself ($$n*n$$), the final number will be the square of that number and finding the square root of a number is inverse operation of squaring the number. If x is the square root of y, it can be expressed as below:

$$x = \sqrt{y}$$

Alternatively, it can also be expressed as:

$$x^2 = y$$

Example: Using exponential (**) operator

The exponential (**) operator of Python can be used to calculate the square root of a number. Please see the below example for syntax.

x = 16
y = 25
x1 = x**0.5
y1 = y**0.5
print("Square root of", x, "is",x1)
print("Square root of", y, "is",y1)

Output

Square root of 16 is 4.0
Square root of 25 is 5.0

Example: Using sqrt() method of math Module

The sqrt() method of math module can also be used to calculate square root of a number.

import math

x = 16
y = 25
x1 = math.sqrt(x)
y1 = math.sqrt(y)
print("Square root of", x, "is",x1)
print("Square root of", y, "is",y1)

Output

Square root of 16 is 4.0
Square root of 25 is 5.0

Example: Using pow() method of math Module

The pow() method of math module can also be used to calculate square root of a number.

import math

x = 16
y = 25
x1 = math.pow(x, 0.5)
y1 = math.pow(y, 0.5)
print("Square root of", x, "is",x1)
print("Square root of", y, "is",y1)

Output

Square root of 16 is 4.0
Square root of 25 is 5.0

Advertisements