Displaying Fibonacci sequences using recursion - Java Object Oriented Design

Java examples for Object Oriented Design:Method

Description

Displaying Fibonacci sequences using recursion

Demo Code

public class Main {
  public static int Fibonacci(int n) {
    if (n <= 1)
      return 1;// www .j a v  a2 s  .  co m
    else
      return Fibonacci(n - 1) + Fibonacci(n - 2);
  }

  public static void main(String[] args) {
    int i;
    for (i = 0; i <= 30; i++)
      System.out.print(Fibonacci(i) + " ");
  }
}

Result


Related Tutorials