beginnerNumber ProblemsJava

Print the Fibonacci Series Up to N Terms

Print the first N terms of the Fibonacci sequence in order.

Quick Answer

Keep two variables for the previous two Fibonacci numbers, starting at 0 and 1. Print the first, then repeatedly compute the next as the sum of the two, shifting the window forward. Repeat until you have printed N terms. This runs in O(n) with no recursion.

Problem Statement

The Fibonacci series starts with 0 and 1, and every later term is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, ....

Given a count N, print the first N terms of the series separated by spaces. Build it iteratively by keeping the last two values and advancing one step at a time — no recursion is needed.

Input: A single integer N (how many terms to print).

Output: The first N Fibonacci numbers separated by single spaces, on one line.

Examples

Example 1
Input:  7
Output: 0 1 1 2 3 5 8

The first seven Fibonacci numbers, each the sum of the previous two.

Example 2
Input:  5
Output: 0 1 1 2 3

Only the first five terms are printed.

Constraints

  • 0 <= N; if N is 0 nothing is printed
  • Use a long to hold terms so larger series do not overflow early

Think Before You Code

Reveal the questions to ask yourself first
  • What two values do you need to remember to produce the next term?
  • What are the correct starting values for the series?
  • How do you shift the two remembered values forward after printing each term?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
Track just two numbers: the current term and the next term, starting at 0 and 1.
Hint 2
Print the current term, then update the pair with `next = a + b; a = b; b = next`.
Hint 3
Loop exactly N times so you print precisely N terms, no more.

Approach

Reveal the step-by-step approach

Carry the previous two terms and slide the window forward.

  1. Set a = 0 and b = 1 (the first two terms).
  2. Repeat N times:
    • Print a (the current term).
    • Compute next = a + b.
    • Advance: a = b, then b = next.
  3. The loop runs exactly N times, printing exactly N terms.

Dry Run

Walk through the example step by step

Printing N = 7 terms:

i | print a | next = a+b | new a | new b
--+---------+------------+-------+------
0 | 0       | 1          | 1     | 1
1 | 1       | 2          | 1     | 2
2 | 1       | 3          | 2     | 3
3 | 2       | 5          | 3     | 5
4 | 3       | 8          | 5     | 8
5 | 5       | 13         | 8     | 13
6 | 8       | 21         | 13    | 21

Output: 0 1 1 2 3 5 8

Solution

Reveal the full Java solution
public class FibonacciSeries {
    public static void printFibonacci(int terms) {
        long a = 0;
        long b = 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < terms; i++) {
            if (sb.length() > 0) {
                sb.append(" ");
            }
            sb.append(a);
            long next = a + b;
            a = b;
            b = next;
        }
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        printFibonacci(7); // 0 1 1 2 3 5 8
        printFibonacci(5); // 0 1 1 2 3
    }
}

Only the two most recent terms are needed to produce the next one, so the whole series is generated with two variables and a single pass. Appending a space only before non-first terms avoids a trailing space, and long terms keep the values correct well beyond where int would overflow (around the 47th term).

Time: O(n)Space: O(1) beyond the output buffer

Common Mistakes

  • Starting with the wrong seeds (for example 1 and 1) and shifting the whole series.
  • Updating a before computing next, which corrupts the running sum.

Edge Cases to Test

  • N = 0 prints an empty line (the loop never runs).
  • N = 1 prints just 0, the first term.

Interview Follow-Ups

  • How would you print terms up to a value limit instead of a term count?
  • How would you return the Nth Fibonacci number instead of printing the whole series?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us