00:00

Java Program to Print the Fibonacci Series

Last updated: 3 min read

The Fibonacci series is a sequence in which each number is the sum of the two numbers before it. It starts with 0 and 1:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

For example, 13 = 5 + 8 and 21 = 8 + 13. To print the series, a program only needs to remember the last two numbers.

1. Fibonacci Series Using a for Loop

The variables first and second hold the last two numbers. On each step the program prints first, works out the next number, and moves both variables one place along.

Fibonacci.java
public class Fibonacci {
    public static void main(String[] args) {
        int count = 10;
        long first = 0, second = 1;

        System.out.println("First " + count + " Fibonacci numbers:");
        for (int i = 1; i <= count; i++) {
            System.out.print(first + " ");
            long next = first + second;
            first = second;
            second = next;
        }
        System.out.println();
    }
}
Output
First 10 Fibonacci numbers:
0 1 1 2 3 5 8 13 21 34

2. Fibonacci Series Using a while Loop and User Input

Here the user chooses how many numbers to print.

FibonacciWhile.java
import java.util.Scanner;

public class FibonacciWhile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("How many Fibonacci numbers? ");
        int count = scanner.nextInt();
        scanner.close();

        long first = 0, second = 1;
        int i = 1;
        while (i <= count) {
            System.out.print(first + " ");
            long next = first + second;
            first = second;
            second = next;
            i++;
        }
        System.out.println();
    }
}
Output (with sample input)
How many Fibonacci numbers? 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

3. Fibonacci Series Up to a Given Number

Sometimes you want all Fibonacci numbers that do not exceed a limit, rather than a fixed count. The loop condition then checks the value instead of a counter.

FibonacciUpTo.java
public class FibonacciUpTo {
    public static void main(String[] args) {
        int limit = 100;
        int first = 0, second = 1;

        System.out.println("Fibonacci numbers up to " + limit + ":");
        while (first <= limit) {
            System.out.print(first + " ");
            int next = first + second;
            first = second;
            second = next;
        }
        System.out.println();
    }
}
Output
Fibonacci numbers up to 100:
0 1 1 2 3 5 8 13 21 34 55 89

4. Fibonacci Number Using Recursion

The definition fib(n) = fib(n - 1) + fib(n - 2) translates directly into a recursive method, with fib(0) = 0 and fib(1) = 1 as base cases.

FibonacciRecursion.java
public class FibonacciRecursion {

    static int fib(int n) {
        if (n <= 1) {
            return n;                     // fib(0) = 0, fib(1) = 1
        }
        return fib(n - 1) + fib(n - 2);
    }

    public static void main(String[] args) {
        for (int n = 0; n < 10; n++) {
            System.out.print(fib(n) + " ");
        }
        System.out.println();
    }
}
Output
0 1 1 2 3 5 8 13 21 34
Note: the recursive version recalculates the same values many times, so it becomes very slow above about n = 40. The loop versions are much faster and should be preferred in real programs.

5. Summary

  • Each Fibonacci number is the sum of the two before it, starting from 0 and 1.
  • A loop only needs two variables for the last two numbers.
  • Loop on a counter to print a fixed number of terms, or on the value to stop at a limit.
  • Recursion matches the mathematical definition but is slow for large n; use long for terms beyond the 46th.

Related Programs