Python Square Root Program – Write a Python Program To find Square root of a number. Three Python programs to find square root of a number using Python.
Code of Python Square Root Programs
Program 1:Use Exponentiation Operator
Exponentiation Operator ** in Python is used to raise the first operand/number to power of second number.
# Python Program to find square root of a number # Input number n = float(input('Enter a number: ')) sqroot = n ** 0.5 print('The square root of %0.2f is %0.2f'%(n ,sqroot))
Enter a number: 16
The square root of 16.00 is 4.00
Program 2: Use sqrt() function of Math Module
The sqrt() function is a predefined method of Math module. This function will find the square root of a given number in Python.
Note: import the math module before using sqrt() function.
# Python program to find square root Using the sqrt() function from math import sqrt num = int(input("Enter a number:")) sqroot = sqrt(num) sqroot = round(sqroot,2) print ("The square root of ",num," is ",sqroot)
Enter a number: 81
The square root of 81.00 is 9.00
Program 3: Use builtin pow() function
# Python program to find square root Using the pow() function from math import pow num = int(input("Enter a number:")) sqroot = pow(num,0.5) sqroot = round(sqroot,2) print ("The square root of ",num," is ",sqroot)
pow() function will take two arguments, first is number and second is power of that number. For example pow(8,2) will return 64. It means that pow(number,power) function will calculate the number raised to the given power.
Enter a number:10
The square root of 10 is 3.16
You may also like the following programs:
Similar to Python Square root Program Concept
Perfect Python Tutorial For Beginners
C++ Program Calculate Sum of Squares of Digits of Given Number
Program Area of Triangle Algorithm Flowchart