Python Program to find the square root of a given number
Program
# Python Program to find the square root of a given number
inputNum = float(input('Enter a number: '))
squareRootOfNum = inputNum ** 0.5
print('The square root of %0.3f is %0.3f'%(inputNum ,squareRootOfNum))
This program calculates the square root of a user-provided number using exponentiation.
-
Take User Input
- The program asks the user to enter a number (can be an integer or a decimal).
-
Calculate Square Root
- The square root is found using the exponentiation operator
** 0.5
. - In mathematics, the square root of
x
is the valuey
such thaty * y = x
.
- The square root is found using the exponentiation operator
-
Display Output
- The result is printed with 3 decimal places using
'%0.3f' % (inputNum, squareRootOfNum)
.
- The result is printed with 3 decimal places using
Key Takeaways
- The exponentiation operator
** 0.5
is a simple way to calculate the square root. - The program supports floating-point numbers.
- The output is formatted to display three decimal places.
Output
$ python3 FindSquareRoot.py
Enter a number: 529
The square root of 529.000 is 23.000