TutorialStudyMite
Find square root in python
HHimani Kohli1 min read
Beginner friendly
Track completion, mastery, and revision.
Square root in Python Program
In this program, we will be designing a code to find the square root of a number given by the user in Python.
The function sqrt is generated in order to find the square root of a number passing the number n as an argument in the function.
**Note: The exponent operator ** is used to calculate the power of a number. It is written as a ** b, where a is the base and b is the exponent.
Algorithm:
- Define a function
sqrtthat takes in a single argumentn. - Inside the function, calculate the square root of
nusing the equationn ** 0.5and assign the result to the variablex. - Prompt the user to enter a number and store the input in the variable
n. - Call the
sqrtfunction with the argumentnto calculate the square root of the user-specified number. - Print the result stored in the variable
x. - Exit the program.
Code:
def sqrt(n):
x = n ** 0.5
print(x)
n = int(input("Enter the number whose square root you need to find: "))
sqrt(n)
Output:
Enter the number whose square root you need to find: 25
5.0
Finished reading?