In this article, we will discuss the concept of program to Calculate power of a number using recursion in Java language
In this post, we will learn how to find the power of a number using recursive function in Java language
Program 1
import java.util.Scanner; class CalcPower{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the base number: "); int base=scan.nextInt(); //read base number from user System.out.print("Enter the exponant number: "); int exp=scan.nextInt(); //read exponent number from user int result=find_Power(base, exp); //call the recursive function System.out.println("The power is "+result); //display the result } public static int find_Power(int base, int exp){ //recursive function for calculating power if(exp !=0){ return (base*find_Power(base,exp-1)); } else{ return 1; } } }
When the above code is executed, it produces the following results
Enter the base number: 4 Enter the exponent number: 5 The power is 1024
In the above program, the user can enter the value for the base and powerValue (for raised times). we can find the power of a given number using a recursive function find_Power().
The recursive function used to multiples the bases with itself for powerValue times.
Explanation
3^4=81
thereafter
4*4*4*4*4=1024
Similar post
find the power of a number in C++ using recursion
find the power of a number in C using recursion
find the power of a number in Python using recursion
Suggested for you
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…