Factorial of N
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 < 0?"}
err[/"Print error: no factorial"/]
init["fact = 1, i = 1"]
d1{"i ≤ N?"}
mul["fact = fact × i"]
inc["i = i + 1"]
out[/"Print fact"/]
e(["End"])
s --> in
in --> d0
d0 -->|Yes| err
d0 -->|No| init
init --> d1
d1 -->|Yes| mul
d1 -->|No| out
mul --> inc
inc --> d1
out --> e
err --> ePaste into any Markdown tool that renders Mermaid, such as GitHub.
About this factorial flowchart
The factorial of a non-negative whole number N, written N!, is the product of all whole numbers from 1 to N. So 5! = 1 × 2 × 3 × 4 × 5 = 120, and by definition 0! = 1. Factorials of negative numbers are not defined.
The flowchart is the standard introduction to loops. Two variables are set before the loop starts: fact, which holds the running product and starts at 1, and i, the counter, which also starts at 1. The decision i ≤ N controls the loop. Inside the loop, fact is multiplied by i and i goes up by one, then the arrow returns to the decision. When i passes N, the loop exits and fact is printed.
Starting fact at 1 rather than 0 matters: multiplying by 0 would make every answer 0. Starting at 1 also handles 0! correctly, because the loop never runs and the chart prints 1.
Step by step
- Input. Read N.
- Validate. If N is negative, print an error and end; factorial is not defined for negative numbers.
- Initialize. Set fact = 1 and i = 1 in one process box.
- Loop test. Is i ≤ N? If yes, enter the loop body; if no, exit to the output.
- Loop body. Multiply fact by i, then add 1 to i, and return to the test.
- Output. Print fact and end.
How to make it in flow-chart.io
- Start from the example. Press "Use this template" to open this exact factorial 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 = 5: fact goes 1, 2, 6, 24, 120 as i goes 1 to 5; i becomes 6, the test fails, and 120 is printed.
- Trace N = 0: i = 1 is not ≤ 0, so the loop never runs and 1 is printed, which is 0!.
- Pseudocode: read N; if N < 0 print error and stop; fact = 1; for i = 1 to N: fact = fact × i; print fact.
- Factorials grow fast: 13! is over 6 billion and does not fit in a 32-bit integer, so use a 64-bit or arbitrary-size number type for larger N.
- A recursive version (fact(N) = N × fact(N-1), fact(0) = 1) is usually drawn as a separate flowchart with a call to itself.
Frequently asked questions
- What is the flowchart for factorial of a number?
- Start, read N, reject negatives, set fact = 1 and i = 1, then loop while i ≤ N: multiply fact by i and add 1 to i. When the loop ends, print fact and stop.
- Why does fact start at 1?
- Because it is a product. Starting at 0 would make every result 0. Starting at 1 also gives the correct answer for 0!, which is 1.
- What symbol is used for the loop?
- There is no separate loop symbol in a basic flowchart. A loop is a decision (diamond) with an arrow that returns to an earlier step. Some textbooks use a hexagon (preparation symbol) for a for-loop header.
- How do I write it in Python?
- fact = 1, then for i in range(1, n + 1): fact *= i, then print(fact). Python's integers grow as needed, so large N works.
- Can I edit this factorial flowchart?
- Yes. Press Use this template to open it in the editor with a free account.