Recursive implementation of the factorial function - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Recursive implementation of the factorial function

Demo Code

public class Main {
  public static int Factorial(int n) {
    if (n == 0)/* www. ja va 2 s.  c o m*/
      return 1; // Terminal stage
    else
      return n * Factorial(n - 1); // Apply recurrence equation
  }

  public static void main(String[] arg) {
    System.out.println("10!=" + Factorial(10));
  }
}

Result


Related Tutorials