First of all you need to know what is Fibonacci series? It is a series where next term is the sum of previous two terms. The first number of the series is 0 and 2nd number of the series is 1.

The Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

In this example we are going to show two different ways of creating Fibonacci series. using for loop and by using while loop.

#### Display Fibonacci series using for loop

public class FibonacciSeries { public static void main(String[] args) { int num = 12, t1 = 0, t2 = 1; System.out.print("series of" + num + " terms: "); for (int i = 1; i <= num; ++i) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; } } }

##### Output

series of 12 terms:0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 +55 + 89 +

Above program prints the Fibonacci series for 12 terms where we defined 1st term as 0 and 2nd term as 1. We are iterating using for loop to and adding previous two terms.

#### Fibonacci series using while loop

public class FibonacciSeries { public static void main(String[] args) { int i = 1, n = 12, t1 = 0, t2 = 1; System.out.print("Series of " + n + " terms: "); while (i <= n) { System.out.print(t1 + " + "); int sum = t1 + t2; t1 = t2; t2 = sum; i++; } } }

##### Output

Series of 12 terms:0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 +55 + 89 +

The output of this program will be same, only thing is we have made use of while loop.