Print the Fibonacci Series Up to N Terms
Print the first N terms of the Fibonacci sequence in order.
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
Input: 7
Output: 0 1 1 2 3 5 8The first seven Fibonacci numbers, each the sum of the previous two.
Input: 5
Output: 0 1 1 2 3Only the first five terms are printed.
Constraints
0 <= N; if N is 0 nothing is printedUse 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
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Carry the previous two terms and slide the window forward.
- Set
a = 0andb = 1(the first two terms). - Repeat
Ntimes:- Print
a(the current term). - Compute
next = a + b. - Advance:
a = b, thenb = next.
- Print
- The loop runs exactly
Ntimes, printing exactlyNterms.
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).
O(n)Space: O(1) beyond the output bufferCommon 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 →