0
1.4kviews
Write a recursive method to calculate factorial of an integer number.

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 5 M

Year: May 2014

1 Answer
0
2views

Given below is another program where the number whose factorial is to be calculated is taken as an input from the user and the result is displayed on the screen.

public class Factorial {
   public static void main(String[] args) {
       int n = 5;
       int result = factorial(n);
       System.out.println("The factorial of 5 is " + result);
   }
   public static int factorial(int n) {
       if (n == 0) {
          return 1;
       } 
       else {
          return n * factorial(n - 1);
       }
   }//end of factorial(int n)
}//end of class


Output:  The factorial of 5 is 125
Please log in to add an answer.