Classic Factorial with non-recursive solution - Java Object Oriented Design

Java examples for Object Oriented Design:Method Recursive

Description

Classic Factorial with non-recursive solution

Demo Code

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

  private static long factorial(int n) {
    long f = 1;
    for (int i = 1; i <= n; i++)
      f = f * i;
    return f;
  }

}

Related Tutorials