HomeJavaJava program to calculate the power using recursion

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

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022