Python Program to Find Power of a Number using For loop
ADVERTISEMENTS
Python program to find power of a number using for loop. In this article, you will learn how to make a python program to find power of a number using for loop.
Syntax to calculate the power of N number
x ^ y = results
Python Program to Find Power of a Number using For loop
# Python Program to Find Power of a Number using For loop
b, x, i, r = None, None, None, 1
# b - To store base number
# x - To store exponent number
# r - To store result value
print ("-----Enter the input of base number-----")
b = int (input ())
print ("\n-----Enter the input of exponent number-----")
x = int (input ())
for i in range (x, 0, -1):
r *= b
print ("\nThe calculation of the power of N number is ", b, "^", x, " = ", r)
Output
-----Enter the input of base number-----
8
-----Enter the input of exponent number-----
3
The calculation of the power of N number is 8 ^ 3 = 512
Python Program to Find Power of a Number using math.pow() function
Note: You have to load the math library to call the
math.pow()
function.
# Python Program to Find Power of a Number using math.pow() function
import math
# math library loaded to call the math.pow() function
b, x, r = None, None, 1
# b - To store base number
# x - To store exponent number
# r - To store result value
print ("-----Enter the input of base number-----")
b = float (input ())
print ("\n-----Enter the input of exponent number-----")
x = float (input ())
r = math.pow(b, x)
print ("\nThe calculation of the power of N number is ", b, "^", x, " = ", r)
Output
-----Enter the input of base number-----
12
-----Enter the input of exponent number-----
4.6
The calculation of the power of N number is 12.0 ^ 4.6 = 92094.50794432325