Python Program to Find Power of Number

Python program to find or calculate power of number; Through this tutorial, i am going to show you how to find or calculate power of number in python.

Python Program to Find or Calculate Power of Number

Let’s see the following programs to find or calculate power of number; as shown below:

  • Python program to find power of a number using for loop
  • Python program to find power of a number using while loop
  • Python program to find power of a number using recursion
  • Python program for exponentiation of a number

Python program to find power of a number using for loop

num = int(input("Enter the number of which you have to find power: "))
pw = int(input("Enter the power: "))
CP = 1
for n in range(pw):
    CP = CP*num
print("The output " , CP)

Output of the above program; as shown below:

Enter the number of which you have to find power:  5 
Enter the power:  2 
The output  25 

Python program to find power of a number using while loop

num = int(input("Enter the number of which you have to find power: "))
pw = int(input("Enter the power: "))
# calculating power using exponential oprator (**)
power = 1
i = 1
while(i <= pw):
    power = power * num
    i = i + 1
print (num, " to the power of ", pw, " is = ", power)

Output of the above program; as shown below:

Enter the number of which you have to find power:  2 
Enter the power:  4 
2  to the power of  4  is =  16 

Python program to find power of a number using recursion

def powerRecursive(num,pw):#function declaration 
    if(pw==1):
        return(num)
    if(pw!=1):
        return (num*power(num,pw-1))
        
num = int(input("Enter the number of which you have to find power: "))
pw = int(input("Enter the power: "))
# calculating power using exponential oprator (**)
result = powerRecursive(num,pw)
print (num, " to the power of ", pw, " is = ", result)

Output of the above program; as shown below:

Enter the number of which you have to find power:  10 
Enter the power:  2 
10  to the power of  2  is =  100 

Python program for exponentiation of a number

num = int(input("Enter the number of which you have to find power: "))
pw = int(input("Enter the power: "))
# calculating power using exponential oprator (**)
result = num**pw
print (num, " to the power of ", pw, " is = ", result)

Output of the above program; as shown below:

Enter the number of which you have to find power:  4 
Enter the power:  3 
4  to the power of  3  is =  64 

Recommended Python Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published.