Python: Program to Find Power of Number

Python: Program to Find Power of Number

The objective of this Python post, you will see various Python programs that cover the following:

  • 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

Now let’s see each one by one:

1: 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

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

2: 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

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

3: 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

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

4: 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

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

Leave a Reply

Your email address will not be published. Required fields are marked *