
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
Java program to calculate the power using recursion
To calculate the power of a number, Java provides different ways to do this using loops, built-in methods, and recursion. In this article, we will learn how to calculate the power of a number using recursion.
In Java, Recursion is a process in which a function calls itself multiple times until it reaches a base condition, and the corresponding function is called a recursive function.
Problem Statement
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
Calculating the Power Using Recursion
Below are the different ways 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.
-
Define a class (e.g., PowerUsingRecursion) that contains the main method and the recursive method.
-
Create a method that uses recursion to calculate the power by repeatedly multiplying the base number until the exponent reaches zero.
-
Create a recursive method that checks if the exponent is 0. If so, return 1 as the base case. Otherwise, return base * power(base, exponent - 1).
-
Pass the user inputs to the recursive method to compute the power of the base raised to the exponent.
-
Display the output.
Example
Here, the input is being entered by the user based on a prompt -
import java.util.Scanner; public class PowerUsingRecursion { // Recursive method to calculate base^exponent public static int power(int base, int exponent) { if (exponent == 0) { return 1; // Base case } return base * power(base, exponent - 1); // Recursive case } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Taking input from user System.out.print("Enter the base: "); int base = scanner.nextInt(); System.out.print("Enter the exponent: "); int exponent = scanner.nextInt(); // Call the recursive method int result = power(base, exponent); // Display the result System.out.println(base + " raised to the power " + exponent + " is: " + result); } }
Output
Enter the base: 2 Enter the exponent: 3 2 raised to the power 3 is: 8
Using Predefined Values
Following are the steps to calculate the power using recursion based on user-defined input.
-
Declare and initialize integer variables for base and exponent.
-
Create a method named power() that returns 1 when the exponent is 0 (base case), and otherwise returns base * power(base, exponent - 1).
-
Pass the defined base and exponent values to the recursive method to compute the power of the base raised to the exponent.
-
Display the output.
Example
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class PowerUsingRecursion { // Recursive method to calculate power public static int power(int base, int exponent) { if (exponent == 0) { return 1; // Base case } return base * power(base, exponent - 1); // Recursive step } public static void main(String[] args) { int base = 3; // base value defined directly int exponent = 4; // exponent value defined directly int result = power(base, exponent); // calling recursive method System.out.println(base + " raised to the power " + exponent + " is: " + result); } }
Output
3 raised to the power 4 is: 81