In this tutorial, you will learn how to display the Fibonacci series in Java using the “for loop.” The Fibonacci series is a series of integers where the next number is the sum of the previous two numbers. The first 2 number of the series are 0 and 1.

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

Display Fibonacci series in Java using ‘for loop’

In the example below, we will display the series up to the number 100.

public class Main {

    public static void main(String[] args) {

        int numberOfTerms = 20; 
        int Term1 = 0;
        int Term2 = 1;
        System.out.print("First 20 terms: ");

        for (int i = 1; i <= numberOfTerms; ++i)
        {
            System.out.print(Term1 + ", ");

            int sum = Term1 + Term2;
            Term1 = Term2;
            Term2 = sum;
        }
    }
}

Output

First 20 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181,

Related articles

Last modified: March 27, 2019