0
0
CProgramBeginner · 2 min read

C Program to Find Power of a Number

You can find the power of a number in C by using a loop to multiply the base number by itself exponent times, like for (int i = 1; i <= exponent; i++) result *= base;.
📋

Examples

Inputbase = 2, exponent = 3
Output8
Inputbase = 5, exponent = 0
Output1
Inputbase = 3, exponent = 4
Output81
🧠

How to Think About It

To find the power of a number, think of multiplying the base number by itself as many times as the exponent says. If the exponent is zero, the result is always 1. So, start with 1 and multiply by the base repeatedly until you reach the exponent count.
📐

Algorithm

1
Get the base number and exponent from the user.
2
Initialize a result variable to 1.
3
Repeat multiplying the result by the base, exponent number of times.
4
After the loop ends, the result holds the power value.
5
Print the result.
💻

Code

c
#include <stdio.h>

int main() {
    int base, exponent, result = 1;
    printf("Enter base and exponent: ");
    scanf("%d %d", &base, &exponent);
    for (int i = 1; i <= exponent; i++) {
        result *= base;
    }
    printf("%d to the power %d is %d\n", base, exponent, result);
    return 0;
}
Output
Enter base and exponent: 2 3 2 to the power 3 is 8
🔍

Dry Run

Let's trace base=2 and exponent=3 through the code

1

Initialize result

result = 1

2

First loop iteration

result = 1 * 2 = 2

3

Second loop iteration

result = 2 * 2 = 4

4

Third loop iteration

result = 4 * 2 = 8

5

Loop ends

result = 8

Iterationresult
0 (initial)1
12
24
38
💡

Why This Works

Step 1: Start with result = 1

We use 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply result by base repeatedly

Each loop multiplies the current result by the base, building up the power step by step.

Step 3: Loop runs exponent times

This ensures the base is multiplied by itself exactly exponent times, giving the correct power.

🔄

Alternative Approaches

Using recursion
c
#include <stdio.h>

int power(int base, int exponent) {
    if (exponent == 0) return 1;
    return base * power(base, exponent - 1);
}

int main() {
    int base, exponent;
    printf("Enter base and exponent: ");
    scanf("%d %d", &base, &exponent);
    printf("%d to the power %d is %d\n", base, exponent, power(base, exponent));
    return 0;
}
Recursion is elegant but can be less efficient and uses more memory due to function calls.
Using math.h pow() function
c
#include <stdio.h>
#include <math.h>

int main() {
    double base, result;
    int exponent;
    printf("Enter base and exponent: ");
    scanf("%lf %d", &base, &exponent);
    result = pow(base, exponent);
    printf("%.2lf to the power %d is %.2lf\n", base, exponent, result);
    return 0;
}
Using pow() is simple and handles floating-point numbers but requires including math.h and linking math library.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs exponent times, so the time grows linearly with the exponent.

Space Complexity

Only a few variables are used, so space is constant regardless of input size.

Which Approach is Fastest?

Using the built-in pow() function is fastest and handles more cases, but the loop method is simple and clear for integers.

ApproachTimeSpaceBest For
Loop multiplicationO(n)O(1)Simple integer powers
RecursionO(n)O(n)Learning recursion, small exponents
math.h pow()O(1)O(1)Floating-point and complex powers
💡
Remember that any number to the power 0 is always 1.
⚠️
Beginners often forget to initialize the result to 1, causing incorrect results.