Java Program to calculate the power using recursion


In this article, we will understand how to calculate the power using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.

A recursive function is a function that calls itself multiple times until a particular condition is satisfied.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter the number and its power 2 and 5

Output

The desired output would be −

The result of 2^5 is 32

Algorithm

Step 1 - START
Step 2 - Declare three integer values namely my_power, my_input and result
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘getPower is defined which takes two integer as input and returns the product value of the input value with itself ‘my_power’ number of times.
Step 5 - The function is called recursively until the value of ‘my_power’ is greater than 0. Store the result.
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class Power {
   public static void main(String[] args) {
      int my_power, my_input, result;
      my_input = 2;
      my_power = 5;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input = my_scanner.nextInt();
      System.out.print("Enter the power value : ");
      my_power = my_scanner.nextInt();
      result = getPower(my_input, my_power);
      System.out.println("The result of " +my_input + "^" + my_power + " is " + result);
   }
   public static int getPower(int my_input, int my_power) {
      if (my_power != 0) {
         return (my_input * getPower(my_input, my_power - 1));
      }
      else {
         return 1;
      }
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number : 2
Enter the power value : 5
The result of 2^5 is 32

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Power {
   public static void main(String[] args) {
      int my_power, my_input, result;
      my_input = 2;
      my_power = 5;
      System.out.println("The number and its power is defined as " +my_input + " and " +my_power);
      result = getPower(my_input, my_power);
      System.out.println("The result of " +my_input + "^" + my_power + " is " + result);
   }
   public static int getPower(int my_input, int my_power) {
      if (my_power != 0) {
         return (my_input * getPower(my_input, my_power - 1));
      }
      else {
         return 1;
      }
   }
}

Output

The number and its power is defined as 2 and 5
The result of 2^5 is 32

Updated on: 22-Feb-2022

770 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements