Java Program to Display Fibonacci Series

To understand this example, you should have the knowledge of the following Java programming topics:


Display Fibonacci Series

The Fibonacci series is a series where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence are 0 followed by 1.

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Suppose, our first two terms are:

firstTerm =  0
secondTerm = 1

The next terms in the Fibonacci series would be calculated as:

nextTerm = firstTerm + secondTerm; (0 + 1)
firstTerm = secondTerm; (1)
secondTerm = nextTerm; (1)

nextTerm = firstTerm + secondTerm; (1 + 1)
....

Let's now apply this logic in our program.


Example: Display Fibonacci Series Using for Loop

class Main {
  public static void main(String[] args) {

    int n = 10, firstTerm = 0, secondTerm = 1;
    System.out.println("Fibonacci Series till " + n + " terms:");

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

      // compute the next term
      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;
    }
  }
}

Output

Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

In the above program, firstTerm and secondTerm are initialized with 0 and 1 respectively (first two digits of Fibonacci series).

Here, we have used the for loop to

  • print the firstTerm of the series
  • compute nextTerm by adding firstTerm and secondTerm
  • assign value of secondTerm to firstTerm and nextTerm to secondTerm

We can also use a while loop to generate the Fibonacci series in Java.

Example 2: Display Fibonacci series using while loop

class Main {
  public static void main(String[] args) {

    int i = 1, n = 10, firstTerm = 0, secondTerm = 1;
    System.out.println("Fibonacci Series till " + n + " terms:");

    while (i <= n) {
      System.out.print(firstTerm + ", ");

      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;

      i++;
    }
  }
}

Output

Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

The working of this program is the same as the previous program.

And, though both programs are technically correct, it is better to use a for loop in this case. It's because the number of iterations (from 1 to n) is known.


Example 3: Display Fibonacci series up to a given number

class Fibonacci {
  public static void main(String[] args) {

    int n = 100, firstTerm = 0, secondTerm = 1;
        
    System.out.println("Fibonacci Series Upto " + n + ": ");
    
    while (firstTerm <= n) {
      System.out.print(firstTerm + ", ");

      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;

    }
  }
}

Output

Fibonacci Series Upto 100:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 

In this example, instead of displaying the Fibonacci series of a certain number, we are displaying the series up to the given number (100).

For this, we just need to compare the firstTerm with n. And, if firstTerm is less than n, it is printed in the series. Else, the series is completed.

Did you find this article helpful?