1// java program to display fibonacci series using for loop
2public class FibonacciSeriesForLoop
3{
4 public static void main(String[] args)
5 {
6 int f = 10, num1 = 0, num2 = 1;
7 System.out.print("First " + f + " fibonacci sequence: ");
8 for(int a = 1; a <= f; ++a)
9 {
10 System.out.print(num1 + " + ");
11 int total = num1 + num2;
12 num1 = num2;
13 num2 = total;
14 }
15 }
16}
1// WARNING: The function assumes that 'fib(0)=0' and 'fib(1)=1'
2// Returns the number of the fibonacci sequence at index 'n'
3public static int fib(int n) {
4 if (n < 2) // No difference if '<' or '<=', because 'fib(2)=2'.
5 return n;
6 return fib(n-1) + fib(n-2); // Uses the recursion method for solving
7}
1public class Fibonacci {
2
3 public static void main(String[] args) {
4
5 int n = 10, t1 = 0, t2 = 1;
6 System.out.print("First " + n + " terms: ");
7
8 for (int i = 1; i <= n; ++i)
9 {
10 System.out.print(t1 + " + ");
11
12 int sum = t1 + t2;
13 t1 = t2;
14 t2 = sum;
15 }
16 }
17}
1import info1.*;
2
3public class FibonacciDemo1{
4
5 public static void main(String[] args){
6
7 System.out.print("Geben Sie ein Zahl an: ");
8
9 int a = Console.in.readInt();
10
11 System.out.println("fib("+a+") = " + fibonacci(a));
12
13 }
14
15 private static int fibonacci(int a){
16
17 if (a==1||a==2) return 1;
18
19 else return fibonacci(a-1)+fibonacci(a-2);
20
21 }
22
23}
24
25
1// fibonacci series in java using while loop
2public class FibonacciSeriesWhileLoop
3{
4 public static void main(String[] args)
5 {
6 int a = 1, num = 15, num1 = 0, num2 = 1;
7 System.out.print("First " + num + " fibonacci sequence: ");
8 while(a <= num)
9 {
10 System.out.print(num1 + " , ");
11 int total = num1 + num2;
12 num1 = num2;
13 num2 = total;
14 a++;
15 }
16 }
17}
1// 1 2 3 5 8 13 21 34
2
3public class Fibonacci {
4
5 public static void main(String[] args) {
6
7 int n = 10, t1 = 0, t2 = 1;
8 System.out.print("First " + n + " terms: ");
9
10 for (int i = 1; i <= n; ++i)
11 {
12 System.out.print(t1 + " + ");
13
14 int sum = t1 + t2;
15 t1 = t2;
16 t2 = sum;
17 }
18 }
19}