0
0
JavaProgramBeginner · 2 min read

Java Program to Calculate Power Using Recursion

You can calculate power using recursion in Java by defining a method like power(base, exponent) that returns 1 if exponent is 0, otherwise returns base * power(base, exponent - 1).
📋

Examples

Inputbase=2, exponent=3
Output8
Inputbase=5, exponent=0
Output1
Inputbase=3, exponent=1
Output3
🧠

How to Think About It

To find the power of a number using recursion, think of the problem as multiplying the base by itself exponent times. If the exponent is zero, the answer is always 1. Otherwise, multiply the base by the result of the same function with the exponent decreased by one.
📐

Algorithm

1
Get the base 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 function called with exponent minus one.
4
Return the result.
💻

Code

java
public class PowerRecursion {
    public static int power(int base, int exponent) {
        if (exponent == 0) {
            return 1;
        } else {
            return base * power(base, exponent - 1);
        }
    }

    public static void main(String[] args) {
        int base = 2;
        int exponent = 3;
        System.out.println("Result: " + power(base, exponent));
    }
}
Output
Result: 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 recursion stops when the exponent is 0, returning 1 because any number to the power 0 is 1.

Step 2: Recursive call

For exponent greater than 0, the function calls itself with exponent minus 1 to break down the problem.

Step 3: Multiplying results

Each recursive call multiplies the base by the result of the smaller problem, building up the final answer.

🔄

Alternative Approaches

Using Math.pow()
java
public class PowerUsingMath {
    public static void main(String[] args) {
        int base = 2;
        int exponent = 3;
        double result = Math.pow(base, exponent);
        System.out.println("Result: " + (int)result);
    }
}
This uses Java's built-in method but is not recursive and returns a double.
Using tail recursion
java
public class PowerTailRecursion {
    public static int power(int base, int exponent) {
        return powerHelper(base, exponent, 1);
    }
    private static int powerHelper(int base, int exponent, int result) {
        if (exponent == 0) {
            return result;
        } else {
            return powerHelper(base, exponent - 1, result * base);
        }
    }
    public static void main(String[] args) {
        System.out.println("Result: " + power(2, 3));
    }
}
Tail recursion can be optimized by some compilers to avoid stack overflow.

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 relative to the exponent.

Space Complexity

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

Which Approach is Fastest?

Using Java's built-in Math.pow() is fastest and optimized, but recursive methods are clearer for learning recursion.

ApproachTimeSpaceBest For
Simple RecursionO(n)O(n)Learning recursion
Tail RecursionO(n)O(n) or optimizedAvoiding stack overflow
Math.pow()O(1)O(1)Performance and simplicity
💡
Always include a base case in recursion to avoid infinite calls.
⚠️
Forgetting to reduce the exponent in the recursive call causes infinite recursion.