sqrt() Function Examples

Let’s look at some different uses of math.sqrt() function.

Example 1: Check if Prime or Not

In this example, we are given a number and we are checking if a number is prime or not. Here,run a loop from 2 to sqrt(n) and check if any number in range (2-sqrt(n)) divides n.

Python3




# Python program for practical application of sqrt() function
# import math module
import math
 
# function to check if prime or not
def check(n):
    if n == 1:
        return False
         
        # from 1 to sqrt(n)
    for x in range(2, (int)(math.sqrt(n))+1):
        if n % x == 0:
            return False
    return True
 
# driver code
n = 23
if check(n):
    print("prime")
else:
    print("not prime")


Output

prime

Example 2: Finding Hypotenuse Of a Triangle

In this example, we are using sqrt() function to find the hypotenuse of a triangle.

Python3




a = 10
b = 23
 
import math
 
# importing the math module
c = math.sqrt(a ** 2 + b ** 2)
 
print( "The value for the hypotenuse would be ", c)


Output

The value for the hypotenuse would be  25.079872407968907

Python math.sqrt() function | Find Square Root in Python

sqrt() function returns square root of any number. It is an inbuilt function in Python programming language.

In this article, we will learn more about the Python Program to Find the Square Root.

Similar Reads

sqrt() Function

We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root of different numbers by using sqrt() function....

Definition of math.sqrt() Function

...

math.sqrt() Method Syntax

sqrt() function in Python is an in-built function, and it is present in the math library....

sqrt() Function Examples

math.sqrt(x)...

sqrt() Function Error

Let’s look at some different uses of math.sqrt() function....