java fibonacci program 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: program to get the Fibonacci series
public class Fibonacci {
public static void main(String[] args) {
int i = 1, n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
while (i <= n)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
i++;
}
}
}