Calculations

Calculate power of a number using recursion in Java

Program to Calculate power of a number using recursion in Java

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

Java method

Operator in Java

If statements in Java

 

 

 

Java code to Calculate average of odd and even in an array
Use of C++ program to find sum of two numbers using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago