English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)/O)

Java Reader/Writer

Java other topics

Java program displays the Fibonacci sequence

Java Examples Comprehensive

In this program, you will learn to use for and while loops in Java to display the Fibonacci sequence. You will learn how to display a series containing a specific number of terms or numbers.

The Fibonacci sequence is a series in which the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0, followed by1.

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

Example1Using for loop to display the Fibonacci sequence

public class Fibonacci {
    public static void main(String[] args) {
        int n = 10, t1 = 0, t2 = 1;
        System.out.print("First " + n + " terms: ");
        for (int i = 1; i <= n; ++i)
        {
            System.out.print(t1 + " + ");
            int sum = t1 + t2;
            t1 = t2;
            t2 = sum;
        }
    }
}

When running the program, the output is:

0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 +

In the above program, the first term (t1) and the second term (t2) are initialized to 0 and1of the first two terms.

Then, the for loop iterates to n (number of terms), displaying the value stored in the variable t1of the first two terms.

You can also use the while loop in Java to generate the Fibonacci sequence.

Example2Using while loop to display the Fibonacci sequence

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

The output is the same as the above program.

In the above program, unlike the for loop, we must increase the value of i inside the loop body.

Although both programs are technically correct, it is best to use a for loop in this case. This is because the number of iterations (from1to n) is known.

Example3: Display the most given numbers (not items) of the Fibonacci sequence

public class Fibonacci {
    public static void main(String[] args) {
        int n = 10, t1 = 0, t2 = 1;
        
        System.out.print("Upto ", + n + :");
        while (t1 <= n)
        {
            System.out.print(t1 + " + ");
            int sum = t1 + t2;
            t1 = t2;
            t2 = sum;
        }
    }
}

When running the program, the output is:

Upto 100: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 + 89 +

This program will display the sequence up to the given number (100), rather than display the sequence until a specific number.

For this, we just need to compare the last two numbers (t1). And n sum.

If t1Print t if less than or equal to n1.

Java Examples Comprehensive