
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Power Using Recursion in Java
In this article, we will understand how to calculate the power of a number using a recursive function in Java. We'll use the Power class and the getPower method to achieve this. The getPower method will be designed to call itself recursively until the power is reduced to zero, at which point it will return the final result.
We'll start by prompting the user to input a base number and its power, and then we'll compute the result using recursion.
Problem Statment
Given a base number and a power value, we need to calculate the result of raising the base to the power using recursion. Below is a demonstration of the same ?
Input
Enter the number and its power 2 and 5
Output
The result of 2^5 is 32
Different approaches to calculate the power using recursion
Below are the different approaches to calculate the power using recursion ?
Using user defined input
Following are the steps to calculate the power using recursion based on user defined input ?
- Import the Scanner class from the java.util package to handle user input.
- Creating the Power class that contains the main method.
- Initialize variables by declaring the variables my_input, my_power, and result to store the base number, power value, and the result.
- Instantiate a Scanner object to read input from the user.
- Prompt user for Input to ask the user to enter the base number and the power value.
- Capture user input use the nextInt() method of the Scanner object to capture the base number and power value entered by the user.
- Calculate power using recursion by calling the getPower method, passing the base number and power value as arguments to compute the result recursively.
- Display the final result.
Example
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
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
Using predefined values
Following are the steps to calculate the power using recursion based on user defined input ?
- Creating the Power class that contains the main method.
- Declare and initialize variables my_input, my_power, and result with predefined values for the base number and power.
- Print the predefined base number and power value to the console.
- Calculate power using recursion by calling the getPower method with the predefined base number and power value as arguments to compute the result recursively.
- Display the result..
Example
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