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: 2020-02-18T09:55:44+05:30

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements