Java program to calculate the power using recursion

In this Java program, we will learn how to calculate the power using recursion in your Java program

How to calculate the power using recursion in JAVA? 

Example 1:  using recursion 

RUN CODE SNIPPET
class Main  
{ 
  public static void main(String[] args)  
  {     
    int base = 3, powerRaised = 5; 
    int result = power(base, powerRaised); 
    System.out.println(base + "^" + powerRaised + "=" + result); 
  } 
  public static int power(int base, int powerRaised)  
  { 
    if (powerRaised != 0)  
    { 
      return (base * power(base, powerRaised - 1)); 
    } 
    else { 
      return 1; 
    } 
  } 
}

OUTPUT 

3^5=243

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024