Fibonacci calculation using terminal recursion - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Fibonacci calculation using terminal recursion

Demo Code

public class Main {

  static int FibonacciRecTerm(int n, int i, int a, int b) {
    if (n == i)//from w  ww.  jav  a  2s . co  m
      return a;
    else
      return FibonacciRecTerm(n, i + 1, a + b, a);
  }

  static int FibonacciLaunch(int n) {
    if (n <= 1)
      return n;
    else
      return FibonacciRecTerm(n, 0, 0, 1);
  }

  public static void main(String[] arg) {
    System.out.println("Fibonacci(7)=" + FibonacciLaunch(7));
  }
}

Result


Related Tutorials