0
0
CProgramBeginner · 2 min read

C Program to Find Power of 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, multiply the base number by itself repeatedly as many times as the exponent says. If the exponent is zero, the result is always 1 because any number to the power of zero equals 1.
📐

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 number, exponent 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 the example where base = 2 and exponent = 3 through the code.

1

Input

base = 2, exponent = 3

2

Initialize result

result = 1

3

First loop iteration

result = 1 * 2 = 2

4

Second loop iteration

result = 2 * 2 = 4

5

Third loop iteration

result = 4 * 2 = 8

6

End loop and print

Output: 2 to the power 3 is 8

IterationResult
12
24
38
💡

Why This Works

Step 1: Initialize result to 1

We start with result = 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply in loop

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

Step 3: Loop runs exponent times

The loop runs exactly exponent times to multiply the base the correct number of times.

🔄

Alternative Approaches

Using pow() function from math.h
c
#include <stdio.h>
#include <math.h>

int main() {
    int base, exponent;
    printf("Enter base and exponent: ");
    scanf("%d %d", &base, &exponent);
    double result = pow(base, exponent);
    printf("%d to the power %d is %.0f\n", base, exponent, result);
    return 0;
}
This method is simpler but uses floating-point math and requires including <code>math.h</code>.
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);
    int result = power(base, exponent);
    printf("%d to the power %d is %d\n", base, exponent, result);
    return 0;
}
This method uses recursion to calculate power but can be less efficient for large exponents.

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 usually fastest due to optimized libraries, but the loop method is simple and clear.

ApproachTimeSpaceBest For
Loop multiplicationO(n)O(1)Simple and clear for small exponents
pow() functionO(1) or optimizedO(1)Fast and easy but uses floating-point
RecursionO(n)O(n)Good for learning recursion but less efficient
💡
Remember that any number raised to the power 0 is always 1.
⚠️
Beginners often forget to initialize the result to 1, causing incorrect results.