Fibonacci Series: flowchart, algorithm and program

This program reads n and prints the first n Fibonacci numbers, starting from 0 and 1. Each new number is the sum of the two before it, so we only need to remember the last two values.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: Set a = 0, b = 1, i = 0.
  4. Step 4: If i < n, go to step 5; otherwise go to step 9.
  5. Step 5: Print a.
  6. Step 6: Set next = a + b.
  7. Step 7: Set a = b, b = next.
  8. Step 8: Set i = i + 1. Go to step 4.
  9. Step 9: Print newline.
  10. Step 10: Stop.

Pseudocode

BEGIN
    READ n
    a ← 0, b ← 1, i ← 0
    WHILE i < n DO
        PRINT a
        next ← a + b
        a ← b, b ← next
        i ← i + 1
    END WHILE
    PRINT newline
END

Program in C, Python and Java

Fibonacci Series program in C

#include <stdio.h>

int main() {
    int n, i, a = 0, b = 1, next;
    printf("Enter n: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("%d ", a);
        next = a + b;
        a = b;
        b = next;
    }
    printf("\n");
    return 0;
}

Fibonacci Series program in Python

n = int(input("Enter n: "))
a = 0
b = 1
for i in range(n):
    print(a, end=" ")
    next_term = a + b
    a = b
    b = next_term
print()

Fibonacci Series program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int a = 0, b = 1, next;
        for (int i = 0; i < n; i++) {
            System.out.print(a + " ");
            next = a + b;
            a = b;
            b = next;
        }
        System.out.println();
    }
}

Sample input: n = 7
Output: 0 1 1 2 3 5 8

How it works

  • Start with a = 0 and b = 1. These are the first two numbers of the series.
  • The for loop runs n times (i from 0 to n-1). In each pass it prints a, the current Fibonacci number.
  • Then next = a + b gives the following number in the series.
  • Shift forward: a = b and b = next. Now a and b hold the next pair, ready for the next pass.
  • For n = 7, a takes the values 0, 1, 1, 2, 3, 5, 8, which is exactly the output.

Dry run (trace table)

StepLineBoxnabinextOutput
13Start
26Read n7Enter n:
37a = 0, b = 1, i = 07010
47i < n?7010
58Print a70100
69next = a + b70101
711a = b, b = next71101
87i = i + 171111
97i < n?71111
108Print a711111
119next = a + b71112
1211a = b, b = next71212
137i = i + 171222
147i < n?71222
158Print a712221
169next = a + b71223
1711a = b, b = next72323
187i = i + 172333
197i < n?72333
208Print a723332

Time and space complexity

Time: O(n) · Space: O(1). The loop runs n times doing constant work each time, using only a few variables.

Common mistakes

  • Updating a and b in the wrong order (for example b = next before a = b), which loses a value and breaks the series.
  • Forgetting the temporary variable next and writing a = b; b = a + b, which uses the already changed a.
  • Looping from 1 to n with i <= n or similar, which prints one number too many or too few.

Viva questions

Q1. Why are a and b initialised to 0 and 1?

The Fibonacci series starts 0, 1 and every later term is the sum of the previous two. So a holds the current term to print (0) and b holds the next term (1).

Q2. What is the role of the variable next, and why can't we just write a = a + b?

next temporarily stores a + b. If we overwrote a first, we would lose its old value, and b would not get the correct new term. The order next = a + b; a = b; b = next; shifts the pair forward.

Q3. Why does the loop run from i = 0 to i < n, and what is printed on each pass?

The loop runs exactly n times, and each pass prints one term, the current value of a. Then a and b move one step ahead in the series.

Q4. What does the program print when n = 0, n = 1, and a negative n?

For n = 0 or a negative n, the condition i < n is false at the start, so nothing is printed except the newline. For n = 1 it prints only 0. The program has no input validation.

Q5. Is this an iterative or recursive solution, and why is that better here?

It is iterative, using a for loop. Each term is computed once from the previous two. A naive recursive fib(n-1)+fib(n-2) recomputes the same values many times and takes exponential time.

Q6. What is the time and space complexity of this program?

Time is O(n) because the loop runs n times with constant work per pass. Space is O(1) because only a few integer variables are used, whatever n is.

Q7. What problem can occur for large n, and how would you fix it?

An int overflows after about the 46th term (the value exceeds 2,147,483,647), giving wrong or negative numbers. Using long long or unsigned long long delays this, and a big-number method would be needed beyond roughly the 90th term.

Q8. What would you change to print Fibonacci numbers up to a limit, say all terms less than 100, instead of the first n terms?

Replace the for loop with while (a < 100), keeping the same print and update statements. The stopping condition would then depend on the value of the term, not on a count.

Frequently asked questions

How does the Fibonacci program print n numbers?

A loop runs n times. Each time it prints a, computes next = a + b, then moves a to b and b to next so the pair slides forward.

Why do we need the variable next?

The new value a + b must be saved before a is overwritten. Without next, a = b would destroy the old a and b would get a wrong sum.

What happens if n is 0 or 1?

If n is 0 the loop never runs and nothing is printed. If n is 1 only 0 is printed.

More programs

← All programs

Try AI Flowchart free →