In this python program, we discuss the calculation of the power f a number using For loop, While loop, and Pow function.
Python Program to Calculate Power of a number using for loop
In this python program, we calculate a power a number using for loop.
number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = 1
for i in range(1, exponent + 1):
power = power * number
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
Written below is the OutPut of the Program.
Please Enter any Positive Integer : 8
Please Enter Exponent Value : 9
The Result of 8 Power 9 = 134217728.0
Python Program to Calculate Power of a number using While loop
In this program, we discuss the to find the power of a number using the while loop.
number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = 1
i = 1
while(i <= exponent):
power = power * number
i = i + 1
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
Here is the output of this program.
Please Enter any Positive Integer : 10
Please Enter Exponent Value : 5
The Result of 10 Power 5 = 100000
Python Program to Calculate Power of a number using Pow function
In this python program calculate the power of a number using the pow function. Next, we are using the built-in math function called pow to find the power of a number.
import math
number = int(input(" Please Enter any Positive Integer : "))
exponent = int(input(" Please Enter Exponent Value : "))
power = math.pow(number, exponent)
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
Here is the output of the program.
Please Enter any Positive Integer : 5
Please Enter Exponent Value : 5
The Result of 5 Power 5 = 3125.0