Tuesday, May 8, 2012

Factorial in Java


public class Factorial {

      public static void main(String[] args) {
            Factorial f = new Factorial();
            int num = 6;
            int fact = f.factorial(6);
            System.out.println(num + " factorial is " + fact);
      }

      public int factorial(int n) {
            if (n == 1)
                  return 1;
            return n * factorial(n - 1);
      }
}

No comments:

Post a Comment