0
0
CppProgramBeginner · 2 min read

C++ Program to Calculate Power Using Recursion

You can calculate power using recursion in C++ by defining a function like int power(int base, int exp) { if (exp == 0) return 1; return base * power(base, exp - 1); } which multiplies the base by itself exp times recursively.
📋

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 using recursion, think of the problem as multiplying the base number by itself repeatedly. The key is to reduce the exponent by one each time and multiply the base until the exponent reaches zero, where the result is 1 because any number to the power of zero is 1.
📐

Algorithm

1
Get the base number and exponent as input.
2
Check if the exponent is zero; if yes, return 1.
3
Otherwise, multiply the base by the result of the power function called with exponent minus one.
4
Return the result.
💻

Code

cpp
#include <iostream>
using namespace std;

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

int main() {
    int base = 2, exponent = 3;
    cout << "Power: " << power(base, exponent) << endl;
    return 0;
}
Output
Power: 8
🔍

Dry Run

Let's trace power(2, 3) through the code

1

Initial call

power(2, 3) checks if exponent == 0 (false), returns 2 * power(2, 2)

2

Second call

power(2, 2) checks if exponent == 0 (false), returns 2 * power(2, 1)

3

Third call

power(2, 1) checks if exponent == 0 (false), returns 2 * power(2, 0)

4

Base case

power(2, 0) returns 1 because exponent == 0

5

Return values

power(2, 1) returns 2 * 1 = 2 power(2, 2) returns 2 * 2 = 4 power(2, 3) returns 2 * 4 = 8

CallExponentReturn Value
power(2, 0)01
power(2, 1)12
power(2, 2)24
power(2, 3)38
💡

Why This Works

Step 1: Base Case

The function stops recursion when the exponent is zero by returning 1, because any number raised to the power 0 is 1.

Step 2: Recursive Call

For exponents greater than zero, the function calls itself with the exponent decreased by one, multiplying the base each time.

Step 3: Multiplying Results

Each recursive call multiplies the base by the result of the smaller exponent, building up the final power value.

🔄

Alternative Approaches

Using a loop instead of recursion
cpp
#include <iostream>
using namespace std;

int power(int base, int exp) {
    int result = 1;
    for (int i = 0; i < exp; i++) {
        result *= base;
    }
    return result;
}

int main() {
    cout << "Power: " << power(2, 3) << endl;
    return 0;
}
This method uses iteration and is often faster and uses less memory than recursion.
Using fast exponentiation (divide and conquer)
cpp
#include <iostream>
using namespace std;

int power(int base, int exp) {
    if (exp == 0) return 1;
    int half = power(base, exp / 2);
    if (exp % 2 == 0) return half * half;
    else return base * half * half;
}

int main() {
    cout << "Power: " << power(2, 10) << endl;
    return 0;
}
This method reduces the number of multiplications by splitting the exponent, making it faster for large exponents.

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

Time Complexity

The function calls itself once per decrement of the exponent, so it runs in linear time proportional to the exponent.

Space Complexity

Each recursive call adds a new layer to the call stack, so space used is proportional to the exponent.

Which Approach is Fastest?

Fast exponentiation reduces time to O(log n) by dividing the problem, making it faster than simple recursion or loops for large exponents.

ApproachTimeSpaceBest For
Simple RecursionO(n)O(n)Small exponents, easy to understand
Loop IterationO(n)O(1)Simple and memory efficient
Fast ExponentiationO(log n)O(log n)Large exponents, performance critical
💡
Always include a base case in recursion to avoid infinite calls.
⚠️
Forgetting to reduce the exponent in the recursive call causes infinite recursion.