Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Power using Recursion in C++

Fast recursive power in C++ using exponentiation by squaring: even and odd exponents, negative powers, O(log n) calls, and a dry run for 2^10.

IntermediateRecursion ProgramsExample 5 of 5
power-using-recursion.cpp
1#include <iostream>
2using namespace std;
3
4// Recursive function to calculate power
5double power(double base, int exponent) {
6 // Base cases
7 if (exponent == 0) {
8 return 1;
9 }
10 if (exponent == 1) {
11 return base;
12 }
13
14 // Handle negative exponent
15 if (exponent < 0) {
16 return 1.0 / power(base, -exponent);
17 }
18
19 // Optimized: Divide and conquer
20 // If exponent is even: base^exp = (base^(exp/2))^2
21 // If exponent is odd: base^exp = base * (base^(exp/2))^2
22 if (exponent % 2 == 0) {
23 double half = power(base, exponent / 2);
24 return half * half;
25 } else {
26 double half = power(base, (exponent - 1) / 2);
27 return base * half * half;
28 }
29}
30
31int main() {
32 double base;
33 int exponent;
34
35 cout << "Enter base: ";
36 cin >> base;
37 cout << "Enter exponent: ";
38 cin >> exponent;
39
40 double result = power(base, exponent);
41 cout << base << "^" << exponent << " = " << result << endl;
42
43 // Test various powers
44 cout << "\nVarious powers:" << endl;
45 cout << "2^10 = " << power(2, 10) << endl;
46 cout << "3^5 = " << power(3, 5) << endl;
47 cout << "5^-2 = " << power(5, -2) << endl;
48 cout << "10^0 = " << power(10, 0) << endl;
49
50 return 0;
51}

Sample Output

5^-2 = 0.04

1 / 25.

10^0 = 1

The exponent-0 base case.

In this one

  • How exponentiation by squaring halves the work
  • Why half is computed once and reused
  • How negative exponents become 1 ÷ xⁿ
  • Why the call count is O(log n)

How it works

  1. 1

    Read base and exponent.

  2. 2

    Return 1 for 0 and base for 1.

  3. 3

    For a negative exponent, return 1 / power(base, −exponent).

  4. 4

    Even: half × half. Odd: base × half × half.

  5. 5

    Print the result and four test powers.

Dry run: Power using Recursion

power(2, 10): even → half = power(2, 5)

  1. 1.power(2, 5): odd → half = power(2, 2)
  2. 2.power(2, 2): even → half = power(2, 1) = 2 → 4
  3. 3.power(2, 5) = 2 × 4 × 4 = 32
  4. 4.power(2, 10) = 32 × 32 = 1024
power(2, 10): even → half = power(2, 5)
power(2, 5): odd → half = power(2, 2)
power(2, 2): even → half = power(2, 1) = 2 → 4
power(2, 5) = 2 × 4 × 4 = 32
power(2, 10) = 32 × 32 = 1024

Edge cases

Zero to a negative power

Enter base 0, exponent -1.

It computes 1.0 / 0.0, which gives inf. Doubles do not throw on division by zero.

Huge results

Enter 10 and 400.

The result exceeds the largest double and becomes inf.

Fractional base

Enter 1.5 and 3.

Works: 1.5 × 1.5 × 1.5 = 3.375.

What's going on

This program computes base^exponent with recursion, and it does it the fast way. Instead of multiplying the base n times, it halves the exponent at each step, an idea called exponentiation by squaring.

The base cases are exponent 0, which returns 1, and exponent 1, which returns the base.

A negative exponent is turned into a positive one: x⁻ⁿ = 1 ÷ xⁿ, so power(5, -2) returns 1.0 / power(5, 2) = 1 / 25 = 0.04.

For an even exponent, xⁿ = (x^(n/2))². The code computes half = power(base, exponent / 2) once and returns half * half. Computing it once is the important part; writing power(base, n/2) * power(base, n/2) would make two calls and lose all the speed.

For an odd exponent, xⁿ = x × (x^((n−1)/2))², so it does the same with one extra multiplication by the base.

2^10 goes 10 → 5 → 2 → 1: four calls instead of ten multiplications. The number of calls grows with log₂ n, so 2^1000 needs about ten calls.

The result type is double, so 3^5 prints as 243 and 5^-2 as 0.04. cout shows up to six significant digits, so very large results switch to scientific notation, such as 1.04858e+06 for 2^20.

Two inputs need care. power(0, -1) computes 1.0 / 0, which for doubles gives inf rather than crashing. And an exponent of INT_MIN cannot be negated in an int, so -exponent overflows.

The headers come first. <iostream> is there because it provides cout and cin for input/output operations. Leave one out and the compiler stops at the first name it does not recognize.

Understanding Power Calculation

Power Definition:

base^exponent = base × base × ... × base (exponent times)
Example: 2^8 = 2 × 2 × 2 × 2 × 2 × 2 × 2 × 2 = 256

Optimization Idea:

Instead of multiplying base exponent times
Use divide-and-conquer: base^exp = (base^(exp/2))^2
Reduces recursive calls significantly

Base Cases

Stopping Conditions:

if (exponent == 0) {

return 1; // Any number to power 0 = 1

}

if (exponent == 1) {

return base; // Any number to power 1 = itself

}

What happens here:

Power 0: always returns 1
Power 1: returns base itself
Stops recursion
Essential base cases

Handling Negative Exponents

Negative Exponent:

if (exponent < 0) {

return 1.0 / power(base, -exponent);

}

What happens here:

Negative exponent: base^(-exp) = 1 / base^exp
Converts to positive exponent
Returns reciprocal
Handles negative exponents

Optimized Recursive Case

Even Exponent:

if (exponent % 2 == 0) {

double half = power(base, exponent / 2);

return half * half;

}

What happens here:

base^even = (base^(even/2))^2
Example: 2^8 = (2^4)^2 = 16^2 = 256
Reduces problem by half
More efficient

Odd Exponent:

else {

double half = power(base, (exponent - 1) / 2);

return base * half * half;

}

What happens here:

base^odd = base × (base^((odd-1)/2))^2
Example: 2^9 = 2 × (2^4)^2 = 2 × 256 = 512
Handles odd exponents
Maintains efficiency

Time Complexity

Efficiency:

Naive: O(n) - n multiplications
Optimized: O(log n) - log n recursive calls
Much faster for large exponents
Divide-and-conquer benefit

Example:

2^1000: naive needs 1000 operations
Optimized: only ~10 recursive calls
Significant improvement

When to Use This Approach

Good fits:

Large exponents
Performance-critical code
Mathematical computations
Efficient power calculation
Divide-and-conquer learning

Example Scenarios:

Cryptography (modular exponentiation)
Scientific calculations
Algorithm optimization
Mathematical libraries
Performance-critical applications

Important Considerations

Divide-and-Conquer:

Breaks problem in half
Reduces recursive calls
Logarithmic time complexity
Efficient algorithm

Exponent Types:

Handles positive exponents
Handles negative exponents
Handles zero exponent
Comprehensive coverage

Precision:

Uses double for decimal results
Handles fractional bases
Maintains precision
Suitable for various inputs

return 0;

return 0; closes main and tells the operating system the program finished without an error.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

How many recursive calls does power(2, 16) make after the first?

Pick an answer to check it.

Question 2

Why compute half once instead of power(b, n/2) * power(b, n/2)?

Pick an answer to check it.

Questions people ask

Why not just use pow()?

pow is fine for doubles. This version shows the algorithm, which also works for integers and modular arithmetic.

How is this used in competitive programming?

As modular exponentiation: take % mod after each multiplication to compute huge powers quickly.

What does the plain recursive version cost?

power(b, n) = b × power(b, n − 1) makes n calls, which is O(n).