Recursive factorial method : Recursive Method « Class Definition « Java Tutorial






public class MainClass {
  public static void main(String args[]) {
    for (int counter = 0; counter <= 10; counter++)
      System.out.printf("%d! = %d\n", counter, factorial(counter));

  }

  // recursive declaration of method factorial
  public static long factorial(long number) {
    if (number <= 1) // test for base case
      return 1; // base cases: 0! = 1 and 1! = 1
    else
      // recursion step
      return number * factorial(number - 1);
  }
}
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800








5.10.Recursive Method
5.10.1.Recursion: a method (function) calls itself
5.10.2.The Towers of Hanoi
5.10.3.Recursion: another example
5.10.4.Recursive factorial method
5.10.5.Recursive fibonacci method
5.10.6.Recursive method to find all permutations of a String