Factorial program using Recursion

Recursion — A function call by itself is called recursion

Factorial Program :

public class Recursion {
public static long factorial(long n) {
if  (n <  0) throw new RuntimeException(“Error in factorial”);
else if (n == 0) return 1;
else      return n * factorial(n-1);            // Function calling itself
}

public static void main(String[] args) {

System.out.println(factorial(19));
}

}

7 thoughts on “Factorial program using Recursion

  1. suresh i want a recurrsion program of factorial the program which u have given is not understandable by me.the program which my teacher has given us is somehow different from that one she has used int rec(int); and i can’t find out rec in urs programme so plzzz help me and tell me what to do i have an assigment of the recurrsion program of factorial and table and power base if my concept is not clear then i can’t peform my other assignments

    1. public class FactorialUsingRecursion {
      public static void main(String[] args) {
      FactorialUsingRecursion ff = new FactorialUsingRecursion();
      int finalVal=ff.factorial(1);
      System.out.println(“finalVal——–>”+finalVal);
      }
      int factorial(int n){
      if(n==1){
      return n;
      }
      else{
      return n * factorial(n – 1);
      }
      }
      }

Leave a comment