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}
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}