Site icon Codeforcoding

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

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find whether a number is even or odd

 

C++ program to find a number is even or odd using the function

C++ program to separate Odd and Even numbers from an array

C++ program to display all even and odd numbers from 1 to n

C++ program calculate Average of odd and even numbers in an array 

C++ program to calculate the sum of odd and even numbers

C++ program to find whether a number is even or odd

 

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

Java program to display all even and odd numbers from 1 to n

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

 

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
Exit mobile version