# How to perform square root without using math module in Python?

In this article, we will show you how to perform square root without using a math module in Python. Below are the various methods to accomplish this task:

• Using exponential operator **
• Using Mathematical Logic

## Using exponential operator **

Without using the math module, the simplest approach to find the square root of a number in Python is to use the built-in exponential operator ** (It is an exponent operator because it calculates the power of the first operand to the power of the second operand).

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Create a variable to store the input number.

• Use the exponential operator (**) to get the square root of a number.

• Print the square root of an input number.

### Example

The following program returns the square root of the input number without using math module and using ** operator

# input number
inputNumber = 25

# getting the square root of a number using the exponential operator**
squareRoot = inputNumber**(1/2)

# printing the square root of a number
print("The square root of", inputNumber, "=", squareRoot)


### Output

On executing, the above program will generate the following output −

('The square root of', 25, '=', 1)


In this method, we took a number and calculated its square root with the ** operator, passing the exponent value as (1/2) or 0.5 to it, and then printed it.

## Using Mathematical Logic

This method is similar to binary search.

### Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

• Create a variable to store the input number.

• Initialize the minimum value as 0.

• Initialize maximum value with input number.

• Use the for loop, to repeat the loop 10 times.

• Get the middle value of both the minimum and maximum numbers by adding min and max values and dividing by 2 (Getting the average of the minimum and maximum values).

• Calculate the square of the middle value using the ** operator and store it in a variable.

• Use the if conditional statement to check whether the square value of the middle value is equal to the input number and if it is true then break the code using the break statement (The break statement in Python terminates the current loop and resumes execution at the next statement, just like the traditional break found in C).

• Else check whether the square value of the middle value is greater than the input number and if it is true then set the maximum value as the middle value.

• Else if the square value of the middle value is less than the number take the minimum value as the middle value.

• Print the middle value which is the resultant square root of an input number.

### Example

The following program returns the square root of the input number without using math module and using mathematical logic −

# input number
number = 9

# initializing minimum value as 0
minimum = 0

# initializing maximum value with input number
maximum =number

# repeating the loop 10 times using for loop
for i in range(10):
# getting the middle value
middle =(minimum+maximum)/2
# getting the square of the middle value
squareValue=middle**2
# checking whether the square value of the middle value is equal to the number
if squareValue ==number:
# break the code if the condition is true
break
# checking whether the square value of the middle value is more than the number

# taking max value as the middle value if the condition is true
maximum=middle

# else if the square value of the middle value is less than the number
# taking the min value as a middle
minimum=middle

# printing the middle value which is the resultant square root
print("The resultant square root of", number, "=", middle)


### Output

On executing, the above program will generate the following output −

('The resultant square root of', 9, '=', 4)


## Conclusion

In this article, we learned two different methods for calculating the square root of a given number without using the math module. We also learned how to calculate the square or square root using the exponential operator (**). We also computed the square using a mathematical logic method similar to the binary search.

Updated on: 28-Aug-2023

22K+ Views 