Python Program - Compute the Power of a Number
Here's a Python program to compute the power of a number using a loop:
refer to:theitroad.combase = int(input("Enter the base number: ")) exponent = int(input("Enter the exponent: ")) result = 1 for i in range(exponent): result *= base print(f"{base} raised to the power of {exponent} is: {result}")
This program takes input for the base and exponent, initializes the result
variable to 1, and uses a loop to multiply the base
by itself exponent
number of times. Finally, it prints the result.
Here's an example output for base=2 and exponent=5:
Enter the base number: 2 Enter the exponent: 5 2 raised to the power of 5 is: 32
Note: Python also has a built-in pow()
function that can be used to compute the power of a number. For example: result = pow(base, exponent)
would achieve the same result as the above program.