fibonacchi in java code example

Example 1: fibonacci java

import info1.*;

public class FibonacciDemo1{

  public static void main(String[] args){

    System.out.print("Geben Sie ein Zahl an: ");

    int a = Console.in.readInt();

    System.out.println("fib("+a+") = " + fibonacci(a));

  }

  private static int fibonacci(int a){

    if (a==1||a==2) return 1;

    else return fibonacci(a-1)+fibonacci(a-2);

  }

}

Example 2: fibonacci series in java

// fibonacci series in java using while loop
public class FibonacciSeriesWhileLoop
{
   public static void main(String[] args)
   {
      int a = 1, num = 15, num1 = 0, num2 = 1;
      System.out.print("First " + num + " fibonacci sequence: ");
      while(a <= num)
      {
         System.out.print(num1 + " , ");
         int total = num1 + num2;
         num1 = num2;
         num2 = total;
         a++;
      }
   }
}

Tags:

Java Example