Print the first N Fibonacci numbers
A worked example to adapt. Rename steps, add branches and owners to match how your team actually works.
Mermaid source for this diagram
flowchart TD
s(["Start"])
in[/"Read N"/]
d0{"N < 1?"}
err[/"Print error: N must be 1 or more"/]
init["a = 0, b = 1, i = 1"]
d1{"i ≤ N?"}
out[/"Print a"/]
next["next = a + b"]
shift["a = b, b = next"]
inc["i = i + 1"]
e(["End"])
s --> in
in --> d0
d0 -->|Yes| err
d0 -->|No| init
init --> d1
d1 -->|Yes| out
d1 -->|No| e
out --> next
next --> shift
shift --> inc
inc --> d1
err --> ePaste into any Markdown tool that renders Mermaid, such as GitHub.
About this fibonacci series flowchart
The Fibonacci series starts 0, 1, and every number after that is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 and so on. Printing the first N terms is a standard exercise because it needs a loop and two variables that move forward together.
The flowchart keeps the two most recent numbers in a and b. Each pass prints a, adds a and b to get the next number, then slides the window forward: a takes b's value and b takes the new number. A counter i stops the loop after N terms.
The order of the two assignments matters. If you set a = b first and then compute a + b, you add the wrong pair. Saving the sum in next before moving a and b, as the diagram does, avoids that mistake and is easy to check in a trace.
Step by step
- Input. Read N, the number of terms to print.
- Validate. If N is less than 1 there is nothing to print, so print an error and end.
- Initialize. Set a = 0 and b = 1 (the first two terms) and the counter i = 1.
- Loop test. While i ≤ N, print the current term; when i passes N, the loop ends.
- Next term. Compute next = a + b, then set a = b and b = next.
- Count. Add 1 to i and go back to the loop test.
How to make it in flow-chart.io
- Start from the example. Press "Use this template" to open this exact Fibonacci flowchart in the editor, or edit the text in the generator box and press Generate for a free preview without signing up.
- Match your program. Rename the input, process and output boxes so they use the same variable names as your code. Keep one action per box.
- Check every branch. Trace each diamond with a test value for the Yes exit and one for the No exit, and make sure both reach the end oval.
- Share or export. Share a read-only view link with a classmate or reviewer. Downloads as PNG, SVG or PDF are part of Flow Pro (see pricing).
Tips
- Trace N = 7: the loop prints 0, 1, 1, 2, 3, 5, 8, then i becomes 8, the test fails and the chart ends.
- Trace N = 1: only 0 is printed. Some textbooks start the series at 1, 1; if yours does, set a = 1 and b = 1.
- Pseudocode: read N; if N < 1 print error and stop; a = 0; b = 1; for i = 1 to N: print a; next = a + b; a = b; b = next.
- The recursive definition, fib(n) = fib(n - 1) + fib(n - 2), is short to write but recomputes the same values many times. The loop in this flowchart does each step once.
- To print terms up to a limit instead of N terms, change the decision to a ≤ limit? and drop the counter.
Frequently asked questions
- What is the flowchart for the Fibonacci series?
- Start, read N, set a = 0, b = 1 and i = 1, then loop while i ≤ N: print a, compute next = a + b, set a = b and b = next, and add 1 to i. When the loop ends, stop.
- Why do we need a third variable?
- Because a and b both change in the same step. Saving a + b in next first means the old values are still available when you move them forward. Without it, one of them is overwritten too early.
- Does the Fibonacci series start with 0 or 1?
- Both conventions exist. Many programming courses start with 0, 1. Others start with 1, 1. Change the starting values in the initialize box to match your course.
- How do I write it in Python?
- a, b = 0, 1, then for _ in range(n): print(a); a, b = b, a + b. Python's tuple assignment updates both at once, so it needs no extra variable.
- Can I edit this Fibonacci flowchart?
- Yes. Press Use this template to open it in the editor with a free account, or type your own steps into the text to flowchart tool.