Write a Python program to compute the power of a number using both the built-in pow()
function and manual calculation (using loops or the **
operator). This program will calculate the result of raising a base number to a certain exponent.
Input-Output Examples
plaintext
Example 1:
Input:
Enter the base: 2
Enter the exponent: 3
Output: 2^3 = 8
Example 2:
Input:
Enter the base: 5
Enter the exponent: 4
Output: 5^4 = 625
Algorithm to calculate the Power of a number
- Start
- Take input: Prompt the user to enter the base and exponent values.
- Use the
**pow()**
function:- Compute the power using Python’s built-in
pow(base, exponent)
function.
- Compute the power using Python’s built-in
- Manually calculate the power:
- Use a loop or the
**
operator to calculate the power.
- Use a loop or the
- Display the result: Print the result of raising the base to the exponent.
- End
Python Program to calculate the power of a number (Using **pow()**
Function)
python
# Python program to compute the power of a number using pow() function
# Take input from the user for the base and exponent
base = float(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
# Compute the power using the built-in pow() function
result = pow(base, exponent)
# Display the result
print(f"{base}^{exponent} = {result}")
Python Program to calculate the power of a number (Using **
Operator)
python
# Python program to compute the power of a number using ** operator
# Take input from the user for the base and exponent
base = float(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
# Compute the power using the ** operator
result = base ** exponent
# Display the result
print(f"{base}^{exponent} = {result}")
Python Program to calculate the power of a number (Manual Calculation using Loop)
python
# Python program to compute the power of a number using a loop
# Take input from the user for the base and exponent
base = float(input("Enter the base: "))
exponent = int(input("Enter the exponent: "))
# Initialize result to 1
result = 1
# Loop to multiply base exponent times
for _ in range(exponent):
result *= base
# Display the result
print(f"{base}^{exponent} = {result}")
Code Explanation
- Taking input from the user:
The program uses theinput()
function to prompt the user to enter both the base and the exponent. The base is taken as a float to allow decimal inputs, and the exponent is taken as an integer. - Computing the power using different methods:
- Using
**pow()**
function: The built-inpow(base, exponent)
function is used to calculate the power directly. - Using
******
operator: The**
operator is used to raise the base to the exponent. - Manual calculation (using loop): A loop is used to multiply the base by itself as many times as specified by the exponent, simulating the power operation.
- Using
- Displaying the output:
Theprint()
function is used to display the result of the base raised to the exponent in a formatted manner, using an f-string to show the base, exponent, and result.