Write a Python Program to Find the Square Root

In this tutorial we will learn how to write a Python program to find the square root of a number. We can find square root of a number in different ways. Some of the methods are discussed below :

1. Square Root using Exponent Method :

Num = float(input("Enter a number to find Square Root: "))

Sqrt = Num ** 0.5

print('The square root of %0.2f is %0.2f'%(Num ,Sqrt))

Output :

Enter a number to find Square Root: 81.00
The square root of 81.00 is 9.00

The above program works for positive real numbers. In the above program first we take user input and store it in Num variable. ** is a exponent sign which is used for find out power of a number. A number has power 1/2 or 0.5 then it represent square root of that particular number. In our example Num**0.5 will give square root of Num which is stored in variable Sqrt variable. %0.2f is format specifier.

2. Square Root using math.sqrt() Method :

import math

Num = float(input("Enter a number to find Square Root: "))

SqRt = math.sqrt(Num)

print('The square root of %0.2f is %0.2f'%(Num ,SqRt))

Output :

Enter a number to find Square Root: 121
The square root of 121.00 is 11.00

Note : To use math.sqrt() which is predefined method to find out square root in Python we have to import math module.

3. Square Root using math.pow() method :

import math

Num = float(input("Enter a number to find Square Root: "))

SqRt = math.pow(Num,0.5)

print('The square root of %0.2f is %0.2f'%(Num ,SqRt))

Output:

Enter a number to find Square Root: 999
The square root of 999.00 is 31.61

Note : To use math.pow() which is predefined method in Python we have to import math module.

Share This!

Leave a Reply

Your email address will not be published.