Writing the factorial function using terminal recursion - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Writing the factorial function using terminal recursion

Demo Code

public class Main {

  static long FactorialRecTerminal(int n, int i, int result) {
    if (n == i)/* www.jav  a2s. com*/
      return result;
    else
      return FactorialRecTerminal(n, i + 1, result * (i + 1));
  }

  static long FactorialLaunch(int n) {
    if (n <= 1)
      return n;
    else
      return FactorialRecTerminal(n, 1, 1);
  }

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

Result


Related Tutorials