Online Tutorials & Training Materials | STechies.com
Register Login

Program to Find the Square Root in Python

|| 0

Program to Find the Square Root in Python
Stechies

In Python sqrt() function is an inbuilt function which returns square root of any number given.

Syntax:

math.sqrt(x)

Parameter:  Any number greater then 0 (Required)

Returns: square root of the number given

Examples of Square Root in Python

Here are 6 ways to find square root of a number in python.

1. Using math.sqrt() Method

import math

print("SQUARE ROOT PROGRAM")

val = 12

if val > 0:
 sqr = math.sqrt(val)
 print(sqr)
else:
 print("Please use value grater than 0");

  OUTPUT :

3.4641016151377544

2. Using ‘**’ Operator

Exponent is to raise a number to the given power, in python ‘**’ operator is used to calculate exponent power.

print("--** Operator Program--")

print("exponent of 2 raised to power 4: ",2**4)

OUTPUT :

exponent of 2 raised to power 4:16

3. Using pow() Function

we can use the pow() function too. In pow() function there is two arguments one is base and the second one is the power value to raised value.

print("--pow Function Program--")

import math

print("exponent of 2 raised to power 4: ",math.pow(2,4))

OUTPUT :

exponent of 2 raised to power 4:16.0

4. Using exp() method

Another method is exp(), where it returns the exponent of e, the numeric argument passed in this function. Before calling this method we have to import the math module.

print("--exp Function Program--")

import math

print("The exponent is : ",math.exp(2.1))

OUTPUT

The exponent is :  8.166169912567652

5. Using Logrithms() function

One another way to find out exponent using the logarithms. The log10 and log will give the exponent base 10 or base e, it is the default value.

print("--Logrithms Function Program--")

import math

print("the exponent is : ",math.log(100))

OUTPUT :

the exponent is :  4.605170185988092

6. Using loop

Find out the exponent by using loop (here we use for loop) and pow function with example.

num = int(input(" Please Enter Postive Integer : "))
epont = int(input(" Enter Exponent Value to the power raise to : "))
power = 1
for loo in range(1, epont + 1):
 power = power * num
print("The Result of {0} Power {1} = {2}".format(num, epont, power))



OUTPUT :

Please Enter Postive Integer : 2
Enter Exponent Value to the power raise to : 4
The Result of 2 Power 4 = 16