C Program to Calculate Power of a Number Using Loop

This C program calculates the power of a number by repeatedly multiplying the base value using a loop. It calculates the result of base raised to the exponent without using the built-in pow() function from the math library.

This approach helps beginners understand loops, multiplication operations, variables, and how mathematical formulas can be implemented using programming logic.

Problem Statement

Write a C 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 user enters base = 2 and exponent = 5, the program should calculate 2⁵ = 32.

Concept Overview

The power of a number means multiplying a number by itself a specific number of times.

The mathematical representation is:

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

For example:

3^4 = 3 × 3 × 3 × 3 = 81

In this program, a loop is used to perform the multiplication repeatedly until the exponent count is reached.

Algorithm

  • Start the program.
  • Declare variables for base, exponent, and result.
  • Initialize result with value 1 because multiplying any number by 1 keeps the original value.
  • Read the base number from the user.
  • Read the exponent value from the user.
  • Run a loop from 1 to exponent value.
  • Multiply result with base in each loop iteration.
  • Display the final calculated power value.
  • Stop the program.

Program

C
#include <stdio.h>

int main() {
    int base, exponent;
    long long result = 1;

    printf("Enter base number: ");
    scanf("%d", &base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);

    for(int i = 1; i <= exponent; i++) {
        result = result * base;
    }

    printf("%d^%d = %lld\n", base, exponent, result);

    return 0;
}

How It Works

The program begins by declaring three variables: base stores the number, exponent stores the power value, and result stores the final calculation.

The result variable is initialized with 1 because it acts as the starting value for multiplication.

The for loop executes exactly exponent times. During every iteration, the current result value is multiplied by the base number.

After the loop finishes, the result variable contains the calculated power value.

Dry Run Example

Input: base = 2, exponent = 5

Initial result = 1

Iteration 1: result = 1 × 2 = 2
Iteration 2: result = 2 × 2 = 4
Iteration 3: result = 4 × 2 = 8
Iteration 4: result = 8 × 2 = 16
Iteration 5: result = 16 × 2 = 32

Final Result = 32

Output

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

Explanation

  • The program takes two inputs: the base number and exponent value.
  • The result variable stores the continuously multiplied value.
  • The for loop repeats the multiplication operation exponent times.
  • After completing all iterations, the final result represents base raised to exponent.
  • The program avoids using pow() and demonstrates how power calculation works internally.

Time Complexity

The loop runs exactly exponent times, so the time complexity of this approach is O(n), where n is the exponent value.

The program uses only a few variables, making the space complexity O(1).

Handling Special Cases

  • If the exponent is 0, the result should be 1 because any non-zero number raised to the power of zero is 1.
  • If the base is 0 and exponent is positive, the result will be 0.
  • Negative exponents require floating-point calculations because the result becomes a fraction.

Applications

  • Used in mathematical calculations and scientific programs.
  • Helps understand repeated multiplication logic.
  • Useful for learning loops and arithmetic operations in C programming.
  • Forms the foundation for implementing advanced algorithms such as fast exponentiation.

Advantages

  • Simple and easy to understand for beginners.
  • Does not require external libraries.
  • Demonstrates the practical use of loops.
  • Works efficiently for small exponent values.

Limitations

  • The loop approach becomes slower for very large exponent values.
  • The long long data type has a limited range and may overflow for large results.
  • The current implementation does not support negative exponents.

Improvement Ideas

  • Use the pow() function from the math library for floating-point calculations.
  • Implement fast exponentiation to reduce time complexity from O(n) to O(log n).
  • Add support for negative exponent values.
  • Use input validation to handle invalid user input.
Note: Note: This program is designed for learning purposes and handles non-negative integer exponent values. Advanced implementations can support larger numbers and negative powers.