Java Program to Find Nth Power of a Number by Using Recursion

In the previous article, we have discussed about Java Program to Reverse a String by Using Recursion

In this program we are going to see how to find Nth power of a number using Recursion in Java programming language.

Java Program to Find Nth Power of a Number by Using Recursion

Let’s start with an example to understand it more clearly.

Assume  the base i.e b = 2
Power to the base i.e p = 4
Result = b^p = 2^4 = 2*2*2*2 = 16

Now let’s see different ways to find Nth power of a number by using Recursion.

Method-1: Java Program to Find Nth Power of a Number By Using Static Input and Recursion

Approach:

  • Declare an integer variable say ‘base’ and initialize it.
  • Declare an integer variable say ‘power’ and initialize it.
  • Call a user defined method findPower() method and pass the Integers ‘base’, ‘power’ as parameters.
  • Inside the user defined method we will check if the base is 0 then we will return the vale as 1
  • Else if the base > 0 then we will recursively call findPower() method to get the result “b*findPower(b,p-1)” and return the value to the main() method.
  • Else if the base < 0 then we will recursively call findPower() method to get the result “1/findPower(b, -p)” and return the value to the main() method.
  • Now the value of the user defined method findPower() is stored in a double variable say ‘value’ inside the main() method.
  • Print the result.

Program:

public class Main 
{
    public static void main(String[] args)
    {
        //declare an integer variable ‘base’ and initialize it 
        int base = 2;
        //declare an integer variable ‘power’ and initialize it 
        int power = 4;
        //calling findPower() method and pass the Integers ‘base’, ‘power’ as parameters
        double value = findPower(base, power);
        // print the result
        System.out.println("The value of base "+base+" and power "+power+" is: "+value);
    }

    //recursive function to find the value of power of the base        
    static double findPower(int b, int p)
    {
        // if the power is 0 then return 1
        if(p == 0)
            return 1;
        // if the power is greater than 0 then recursively call findPower() method and return the value
        else if (p>0)
            return b*findPower(b,p-1);
        // if the power is less than 0 then recursively call findPower() method and return the value
        else
            return 1/findPower(b, -p);
    }
}
Output:

The value of base 2 and power 4 is: 16.0

Method-2: Java Program to Find Nth Power of a Number By Using User Input and Recursion

Approach:

  • Declare an integer variable say ‘base
  • Declare an integer variable say ‘power
  • Prompt the user to enter the values for the ‘base’ and ‘power’ respectively.
  • Call a user defined method findPower() method and pass the Integers ‘base’, ‘power’ as parameters.
  • Inside the user defined method we will check if the base is 0 then we will return the vale as 1.
  • Else if the base > 0 then we will recursively call findPower() method to get the result “b*findPower(b,p-1)” and return the value to the main() method.
  • Else if the base < 0 then we will recursively call findPower() method to get the result “1/findPower(b, -p)” and return the value to the main() method.
  • Now the value of the user defined method findPower() is stored in a double variable say ‘value’ inside the main() method.
  • Print the result.

Program:

import java.util.*;
import java.io.*;
public class Main 
{
    public static void main(String[] args)
    {
        // create a scanner class
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the base of the number: ");
        //declare an integer variable ‘base’ and initialize it by user input using scanner class.
        int base = sc.nextInt();
        System.out.println("Enter the power of the number: ");
        //declare an integer variable ‘power’ and initialize it by user input using scanner class.
        int power = sc.nextInt();
        //calling findPower() method and pass the Integers ‘base’, ‘power’ as parameters
        double value = findPower(base, power);
        // print the result
        System.out.println("The value of base "+base+" and power "+power+" is: "+value);
    }

    //recursive function to find the value of power of the base        
    static double findPower(int b, int p)
    {
        // if the power is 0 then return 1
        if(p == 0)
            return 1;
        // if the power is greater than 0 then recursively call findPower() method and return the value
        else if (p>0)
    		return b*findPower(b,p-1);
        // if the power is less than 0 then recursively call findPower() method and return the value
        else
            return 1/findPower(b, -p);
    }
}
Output:

Enter the base of the number: 
2
Enter the power of the number: 
-4
The value of base 2 and power -4 is: 0.0625

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: