Java Program to Calculate the Power Using Recursion

Java Program to Calculate the Power Using Recursion

What is Power of a Number ?

In mathematics, the power of a number refers to the number of times that number is multiplied by itself. It is represented by an exponent, which is a small number written to the right and above the base number. For example, if we have the base number 2 raised to the power of 3, it is written as 2³, which means 2 multiplied by itself 3 times:
2³ = 2 x 2 x 2 = 8

How to Calculate the Power of a Number ? 

To calculate the power of a number, you simply need to multiply the base number by itself the number of times indicated by the exponent. Here’s a general formula for calculating the power of a number:

a^n = a × a × a × … (n times)

where “a” is the base number and “n” is the exponent.

For example, let’s say we want to calculate 2 raised to the power of 4. Using the formula above, we get:

2^4 = 2 × 2 × 2 × 2 = 16

Similarly, if we want to calculate 5 raised to the power of 3, we get:

5^3 = 5 × 5 × 5 = 125

So, to calculate the power of a number, all you need to do is multiply the base number by itself the number of times indicated by the exponent.

Steps to Find the Power of a Number using Recursion: 

  1. Define a function that takes two parameters: the base number and the exponent.
  2. Inside the function, check if the exponent is equal to 0. If it is, return 1.
  3. If the exponent is not equal to 0, recursively call the function with the base number and the exponent minus 1 as arguments.
  4. Multiply the result of the recursive call by the base number and return the result.

Recursion Pseudo Code for the above algorithm :

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

Example 1 : 

Run

public class Main {
    public static void main(String[] args) {
        int base = 2;
        int exponent = 4;
        int result = power(base, exponent);

        System.out.println("2 raised to the power of 4 is: " + result);
    }

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

Output :

2 raised to the power of 4 is: 16

Example 2 : 

Run
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the base number: ");
        double base = scanner.nextDouble();

        System.out.print("Enter the exponent: ");
        double exponent = scanner.nextDouble();

        double result = power(base, exponent);

        System.out.println(base + " raised to the power of " + exponent + " is " + result);
    }

    public static double power(double base, double exponent) {
        if (exponent == 0) {
            return 1;
        } else if (exponent > 0) {
            return base * power(base, exponent - 1);
        } else {
            return 1 / power(base, -exponent);
        }
    }
}

Output :

Enter the base number: 5
Enter the exponent: 2
5.0 raised to the power of 2.0 is 25.0

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription