Write a Python Program to find Power of a Number For Loop, While Loop, and pow function with an example.
Program to find Power of a Number using For loop
This Python program allows the user to enter any numerical value or exponent. Next, this program finds the power of 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))
The Power of a Number using for loop output
Please Enter any Positive Integer : 3
Please Enter Exponent Value : 4
The Result of 3 Power 4 = 81
Python Program to return Power of a Number using While loop
This power of a number program is the same as above, but this time, we are using 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))
using while loop output
Please Enter any Positive Integer : 10
Please Enter Exponent Value : 5
The Result of 10 Power 5 = 100000
Program to find Power of a Number using Pow function
This Python program example accepts the number and an exponent from the user. 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))