Classic Factorial with recursive solution - Java Object Oriented Design

Java examples for Object Oriented Design:Method Recursive

Description

Classic Factorial with recursive solution

Demo Code

public class Main {
  public static void main(String[] args) {
    int n = 5;/*from w  ww .j  a  v  a  2  s .  c om*/
    long fact;
    fact = factorial(n);
    System.out.println("The factorial of " + n + " is " + fact + ".");
  }

  private static long factorial(int n) {
    if (n == 1)
      return 1;
    else
      return n * factorial(n - 1);
  }

}

Related Tutorials