How do we call a Java method recursively?


Recursion is where which a method class itself.

Example

public class RecursionExample {
   private static long factorial(int n) {
   if (n == 1)
      return 1;
   else
      return n * factorial(n-1);
   }

   public static void main(String args[]) {
      RecursionExample obj = new RecursionExample();
      long result = obj.factorial(5);
      System.out.println(result);
   }
}

Updated on: 18-Feb-2020

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements