Python Program to calculate the Power of a number

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

  1. Start
  2. Take input: Prompt the user to enter the base and exponent values.
  3. Use the **pow()** function:
    • Compute the power using Python’s built-in pow(base, exponent) function.
  4. Manually calculate the power:
    • Use a loop or the ** operator to calculate the power.
  5. Display the result: Print the result of raising the base to the exponent.
  6. 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

  1. Taking input from the user:
    The program uses the input() 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.
  2. Computing the power using different methods:
    • Using **pow()** function: The built-in pow(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.
  3. Displaying the output:
    The print() 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.

Python

1514

325

Related Articles