How do we call a Java method recursively?

Java 8Object Oriented ProgrammingProgramming

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);
   }
}
raja
Published on 02-Jan-2018 11:30:26
Advertisements