Python Program to Calculate Power of a Number Using Loop

This Python program calculates the power of a number by repeatedly multiplying the base value using a loop.

Instead of using the built-in pow() function, this program demonstrates how exponent calculations work internally using basic programming logic.

Problem Statement

Write a Python program that accepts a base number and an exponent from the user and calculates the value of base raised to the power of exponent.

For example, if the input base is 2 and exponent is 5, the output should be 2⁵ = 32.

Concept Overview

The power of a number is calculated by multiplying the base value by itself a specific number of times.

base^exponent = base × base × base ... (exponent times)

A loop is used to repeat the multiplication operation until the exponent count is completed.

Algorithm

  • Start the program.
  • Take base and exponent values as input.
  • Create a result variable and initialize it with 1.
  • Run a loop from 1 to exponent value.
  • Multiply result with base during every iteration.
  • Print the final result.
  • Stop the program.

Program

Python
base = int(input("Enter base number: "))
exponent = int(input("Enter exponent: "))

result = 1

for i in range(exponent):
    result *= base

print(f"{base}^{exponent} = {result}")

How It Works

The program takes two inputs from the user: the base number and the exponent value.

The result variable starts with a value of 1 because it is used as the initial multiplication value.

The for loop runs the same number of times as the exponent value and multiplies the result with the base in each iteration.

After completing the loop, the result variable contains the final power value.

Output

Enter base number: 2
Enter exponent: 5
2^5 = 32

Dry Run Example

Input: base = 3, exponent = 4

Initial result = 1

Iteration 1: 1 × 3 = 3
Iteration 2: 3 × 3 = 9
Iteration 3: 9 × 3 = 27
Iteration 4: 27 × 3 = 81

Final Result = 81

Explanation

  • The base value is multiplied repeatedly.
  • The number of repetitions depends on the exponent value.
  • The final multiplication result gives the required power.
  • The program demonstrates the working principle behind exponent calculation.

Time Complexity

The loop executes exponent number of times, making the time complexity O(n), where n represents the exponent.

The program uses only one additional variable, so the space complexity is O(1).

Handling Special Cases

  • Any number raised to the power of 0 returns 1.
  • A positive number raised to a positive exponent works normally.
  • Negative exponents require floating-point calculations.
  • Very large powers may exceed available memory.

Applications

  • Mathematical calculations.
  • Scientific computing programs.
  • Learning loops and arithmetic operations.
  • Understanding exponentiation algorithms.

Advantages

  • Simple beginner-friendly implementation.
  • Does not require external libraries.
  • Helps understand loop execution.
  • Easy to modify for different requirements.

Limitations

  • Slow for extremely large exponent values.
  • Basic version does not handle negative exponents.
  • Large results may consume more memory.

Improvement Ideas

  • Use Python's built-in pow() function.
  • Implement fast exponentiation using divide and conquer.
  • Add validation for user input.
  • Support decimal and negative exponent values.
Note: This example is designed for beginners to understand how exponent calculation works using loops in Python.