Java Program to Calculate Power of a Number using Recursion

In this java program, we will learn about how to calculate the power of a number using recursion.
Here we are using following recurrence relation to calculate AN.

AN = A*A*A*A..... N times
AN = A * AN-1
Let power(A, N) be a method to calculate AN. As per the above recurrence equation, To find power(A, N) we can first calculate power(A, N-1) then multiply it with A. power(A, N) = A * power(A, N-1)

To understand this java program, you should have understanding of the following Java programming concepts:


Java Program to calculate power of a number using recursion

public class PowerOfNumberRecursion {
  // Function to calculate A^N
  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) {
    System.out.println("2^6 = " + power(2, 6));
  }
}
Output
2^6 = 64