19-Oct-2022
.
Admin
Hi Dev,
Here, I will show you how to works Python Program to find Power of a Number . you'll learn Power of a number using Python. this example will help you How to find the power of a number in Python. In this article, we will implement a Python Power: A Step-By-Step Guide. you will do the following things for Python program to find the power of a number using loop.
Python program to find power of a number; In this tutorial, you will learn how to find power of a number in python using for loop, while loop, recursion function, and exponentiation.
Now let’s see each one by one:
Example 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: 10
Enter the power: 2
The output 100
Example 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
Example 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: 5
Enter the power: 2
5 to the power of 2 is = 25
Example 4: Python program for exponentiation of a number
# Python Program to Print Even Numbers from 1 to N using for loop
num = int(input(" Please Enter the Maximum Number : "))
for number in range(1, num+1):
if(number % 2 == 0):
print("{0}".format(number))
Output:
Enter the number of which you have to find power: 3
Enter the power: 3
3 to the power of 3 is = 27
I hope it can help you...
#Python