Python Program - Square Root of a Number
If a number is multiplied by itself (n*n), the final number will be the square of that number and finding the square root of a number is inverse operation of squaring the number. If x is the square root of y, it can be expressed as below:
Alternatively, it can also be expressed as:
x2 = y
Method 1: Using exponential (**) operator
The exponential (**) operator of Python can be used to calculate the square root of a number. See the example below for syntax.
x = 16 y = 25 x1 = x**0.5 y1 = y**0.5 print("Square root of", x, "is",x1) print("Square root of", y, "is",y1)
The above code will give the following output:
Square root of 16 is 4.0 Square root of 25 is 5.0
Method 2: Using sqrt() method of math Module
The sqrt() method of math module can also be used to calculate square root of a number.
import math x = 16 y = 25 x1 = math.sqrt(x) y1 = math.sqrt(y) print("Square root of", x, "is",x1) print("Square root of", y, "is",y1)
The above code will give the following output:
Square root of 16 is 4.0 Square root of 25 is 5.0
Method 3: Using pow() method of math Module
The pow() method of math module can also be used to calculate square root of a number.
import math x = 16 y = 25 x1 = math.pow(x, 0.5) y1 = math.pow(y, 0.5) print("Square root of", x, "is",x1) print("Square root of", y, "is",y1)
The above code will give the following output:
Square root of 16 is 4.0 Square root of 25 is 5.0
Recommended Pages
- Python Program - To Check Prime Number
- Python Program - Bubble Sort
- Python Program - Selection Sort
- Python Program - Maximum Subarray Sum
- Python Program - Reverse digits of a given Integer
- Python - Swap two numbers
- Python Program - Fibonacci Sequence
- Python Program - Insertion Sort
- Python Program - Find Factorial of a Number
- Python Program - Find HCF of Two Numbers
- Python Program - To Check Whether a Number is Palindrome or Not
- Python Program - To Check Whether a String is Palindrome or Not
- Python Program - Heap Sort
- Python Program - Quick Sort
- Python - Swap Two Numbers without using Temporary Variable
- Python Program - To Check Armstrong Number
- Python Program - Counting Sort
- Python Program - Radix Sort
- Python Program - Find Largest Number among Three Numbers
- Python Program - Print Floyd's Triangle