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.
1#include <iostream>2using namespace std;34// Recursive function to calculate power5double power(double base, int exponent) {6 // Base cases7 if (exponent == 0) {8 return 1;9 }10 if (exponent == 1) {11 return base;12 }1314 // Handle negative exponent15 if (exponent < 0) {16 return 1.0 / power(base, -exponent);17 }1819 // Optimized: Divide and conquer20 // If exponent is even: base^exp = (base^(exp/2))^221 // If exponent is odd: base^exp = base * (base^(exp/2))^222 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}3031int main() {32 double base;33 int exponent;3435 cout << "Enter base: ";36 cin >> base;37 cout << "Enter exponent: ";38 cin >> exponent;3940 double result = power(base, exponent);41 cout << base << "^" << exponent << " = " << result << endl;4243 // Test various powers44 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;4950 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
Read base and exponent.
- 2
Return 1 for 0 and base for 1.
- 3
For a negative exponent, return 1 / power(base, −exponent).
- 4
Even: half × half. Odd: base × half × half.
- 5
Print the result and four test powers.
Dry run: Power using Recursion
power(2, 10): even → half = power(2, 5)
- 1.power(2, 5): odd → half = power(2, 2)
- 2.power(2, 2): even → half = power(2, 1) = 2 → 4
- 3.power(2, 5) = 2 × 4 × 4 = 32
- 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:
Optimization Idea:
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:
Handling Negative Exponents
Negative Exponent:
if (exponent < 0) {
return 1.0 / power(base, -exponent);
}
What happens here:
Optimized Recursive Case
Even Exponent:
if (exponent % 2 == 0) {
double half = power(base, exponent / 2);
return half * half;
}
What happens here:
Odd Exponent:
else {
double half = power(base, (exponent - 1) / 2);
return base * half * half;
}
What happens here:
Time Complexity
Efficiency:
Example:
When to Use This Approach
Good fits:
Example Scenarios:
Important Considerations
Divide-and-Conquer:
Exponent Types:
Precision:
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).