
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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 .
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
- Related Articles
- Golang Program to Calculate The Power using Recursion
- C++ Program to Calculate Power Using Recursion
- Java program to calculate the power of a Given number using recursion
- Java program to calculate the GCD of a given number using recursion
- How to calculate Power of a number using recursion in C#?
- Raise x to the power n using Recursion in Java
- Java program to calculate the power of a number
- C++ program to Calculate Factorial of a Number Using Recursion
- Write a C# program to calculate a factorial using recursion
- Java Program to Find G.C.D Using Recursion
- Calculate the Power of a Number in Java Program
- Factorial program in Java using recursion.
- Java program to reverse a string using recursion
- Java Program to Reverse a Sentence Using Recursion
- Fibonacci series program in Java using recursion.
