Python program to enter any number and calculate its square root
ADVERTISEMENTS
Python program to enter any number and calculate its square root.
There are you will learn how to find the square root of any number in the Python language.
Formula:
r = n^(1/2)
where:
r = root
n = number
There are two ways to implement these formulae:
- By using math.sqrt() function
- Second, by using the pow() function
Note: before using math.sqrt() function you have to import the math library in the program.
1. By using the math.sqrt() function
# Python program to calculate the square root of a number
import math
print("Enter any number to find the square root::")
n, r = float(input()), None
# n = number
# r = root
# Calculate square root of number
r = math.sqrt(n)
# Output
print("Square root of ", n, " = ", r)
Output:
Enter any number to find the square root::
25
Square root of 25.0 = 5.0
2. By using the pow() function
# Python program to calculate the square root of a number using the pow() function
print("Enter any number to find the square root::")
n, r = float(input()), None
# n = number
# r = root
# Calculate square root of number
r = pow(n, 0.5)
# Output
print("Square root of ", n, " = ", r)
Output:
Enter any number to find the square root::
25
Square root of 25.0 = 5.0