Java ProgramsNumbersCalculate Power Using Recursion

Calculate Power Using Recursion in Java

intermediate·  Numbers  ·  Recursion

Problem

Computing a power recursively means expressing base^exponent in terms of a smaller instance of the same problem, until a base case is reached.

Given a base and an exponent, compute the result using recursion.

Input
base = 2, exponent = 10
Output
2^10 = 1024

Java Program

Java
public class PowerRecursive { static long power(int base, int exponent) { if (exponent == 0) return 1; // base case: anything to the power 0 is 1 return base * power(base, exponent - 1); } public static void main(String[] args) { int base = 2, exponent = 10; System.out.println(base + "^" + exponent + " = " + power(base, exponent)); } }

Output

2^10 = 1024

Core Logic

Peeling off one multiplication per recursive call, and stopping once the exponent reaches zero, maps the mathematical definition of a power directly onto code.

How It Works
  1. 1The base case if (exponent == 0) return 1; stops the recursion, since anything to the power of 0 is 1.
  2. 2Every other call returns base * power(base, exponent - 1), deferring its own result until the smaller call finishes.
  3. 3Each call reduces the exponent by exactly 1, so the recursion descends one level per remaining multiplication needed.
  4. 4As the calls return, the pending multiplications unwind back up the stack, building the final result.
power(2, 10) unwinds through power(2, 9), power(2, 8), and so on down to the base case, multiplying by 2 at each level on the way back up to reach 1024.
💡

Key Point: This is a direct, one-multiplication-per-call recursion — straightforward to follow, but it makes exactly as many recursive calls as the exponent itself.

Complexity
Time Complexity: O(n)Space Complexity: O(n)

Why: Each call peels off one multiplication and recurses on a smaller exponent, so both the number of calls and the stack depth grow linearly with the exponent.

Key Concepts

recursionbase casemultiplication

Approach 2: Fast Exponentiation (Squaring)

Java
public class PowerFastRecursive { static long power(int base, int exponent) { if (exponent == 0) return 1; long half = power(base, exponent / 2); if (exponent % 2 == 0) { return half * half; // base^n = (base^(n/2))^2 for even n } else { return base * half * half; // one extra factor of base for odd n } } public static void main(String[] args) { int base = 2, exponent = 10; System.out.println(base + "^" + exponent + " = " + power(base, exponent)); } }

Output

2^10 = 1024

Core Logic

Since base^n equals (base^(n/2))² whenever n is even, halving the exponent at every step reaches the base case in far fewer calls than counting down by 1 each time.

How It Works
  1. 1Each call first recurses on exponent / 2, computing half — the result for roughly half the original exponent.
  2. 2If exponent is even, the answer is simply half * half, matching the identity base^n = (base^(n/2))².
  3. 3If exponent is odd, one extra factor of base is needed: base * half * half, to account for the leftover power that halving couldn't evenly split.
  4. 4The same base case, exponent == 0, still stops the recursion.
power(2, 10) only needs to resolve exponent values 10, 5, 2, 1, 0 — five calls deep — instead of the ten levels the naive version needs.
💡

Key Point: Halving the exponent instead of decrementing it is what turns a linear number of calls into a logarithmic one — the same kind of speedup fast-doubling gives Fibonacci.

Complexity
Time Complexity: O(log n)Space Complexity: O(log n)

Why: Each call halves the exponent instead of decrementing it by one, so the recursion depth — and the total number of calls — grows logarithmically with n.

Key Concepts

exponentiation by squaringrecursioneven/odd exponent

Related Programs