Flowchart Updated 12/2/2026

Factorial Flowchart

Flowchart for factorial of a number: start fact at 1, multiply it by every i from 1 to n in a loop, then print it. With a trace for n = 5 (answer 120), pseudocode, tested Python, C and Java code, and the recursive version.

factorialflowchartloopsalgorithmsprogramming
8 nodes 8 connections

Study guide

Factorial FlowchartStartRead nSet fact = 1, i = 1i <= n?Set fact = fact * iSet i = i + 1Print factEndYesNo
Factorial Flowchart. Source: https://www.chartcraft.dev/examples/factorial-flowchart

What the factorial flowchart shows

The factorial of n, written n!, is the product of every whole number from 1 to n, so 5! = 1 * 2 * 3 * 4 * 5 = 120, and 0! is defined as 1. A flowchart for factorial of a number is a counting loop with a running product: fact starts at 1 and is multiplied by each i in turn.

Factorials grow very fast: 10! is already 3,628,800, which is why programs overflow so quickly on them.

Factorial flowchart, step by step

  1. terminator StartOne Start terminator opens the flowchart.
  2. input/output Read nReads the number n whose factorial we want.
  3. process Set fact = 1, i = 1fact starts at 1, not 0, because it is a product: anything times 0 is 0. i is the counter that walks from 1 to n.
  4. decision i <= n?Yes means there is still a number to multiply in. No means the product is complete.
  5. process Set fact = fact * iMultiplies the running product by the current i.
  6. process Set i = i + 1Moves to the next number and loops back to the test.
  7. input/output Print factShows the result. For n = 0 the loop never runs and this prints 1, which is correct.
  8. terminator EndOne End terminator.

Dry run: trace table for n = 5

Each row is one test of i <= n?, including the last one, which fails.

Testii <= n?fact = fact * ii after i + 1Next step
11Yes1 * 1 = 12Back to i <= n?
22Yes1 * 2 = 23Back to i <= n?
33Yes2 * 3 = 64Back to i <= n?
44Yes6 * 4 = 245Back to i <= n?
55Yes24 * 5 = 1206Back to i <= n?
66Nonot runnot runPrint fact (120), then End

5 multiplications and 6 tests. The loop test always runs once more than the body.

Factorial pseudocode

Pseudocode
READ n
fact = 1
i = 1
WHILE i <= n
    fact = fact * i
    i = i + 1
ENDWHILE
PRINT fact

Factorial in Python, C and Java

Python
n = int(input())

fact = 1
i = 1
while i <= n:
    fact = fact * i
    i = i + 1

print(fact)
C
#include <stdio.h>

int main(void) {
    int n;
    scanf("%d", &n);

    long long fact = 1;   /* int overflows after 12! */
    int i = 1;
    while (i <= n) {
        fact = fact * i;
        i = i + 1;
    }

    printf("%lld\n", fact);
    return 0;
}
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        long fact = 1;   // int overflows after 12!
        int i = 1;
        while (i <= n) {
            fact = fact * i;
            i = i + 1;
        }

        System.out.println(fact);
    }
}
Input typed at the keyboard
5
Output of all three programs
120

All three programs were compiled and run with the input 5 before publishing. C and Java use a 64-bit type, which is exact up to 20!. Python integers never overflow.

Mistakes examiners see most

  • Starting fact at 0. Then every multiplication gives 0 and the answer is always 0. A running sum starts at 0, a running product starts at 1.
  • Testing i < n instead of i <= n. The loop then stops before multiplying by n itself, so 5 gives 24 instead of 120.
  • Putting Print fact inside the loop. That prints 1, 2, 6, 24, 120, every partial product. If the question asks for the factorial, print once, on the No exit.
  • Using int in C or Java for big n. 13! is 6,227,020,800, which does not fit in a 32-bit int, so the answer silently goes wrong. Use long long or long.

Practice questions with worked answers

Q1 What does the flowchart print for n = 0, and how many times does the loop body run?

It prints 1, and the body runs 0 times.

The first test is 1 <= 0?, which is No, so the flow goes straight to Print fact while fact is still 1. That matches the rule 0! = 1.

Q2 Rewrite the flowchart so the loop counts down from n to 1. Does the answer change?
Pseudocode
READ n
fact = 1
WHILE n > 1
    fact = fact * n
    n = n - 1
ENDWHILE
PRINT fact

The answer is the same, because multiplication can be done in any order: 5 * 4 * 3 * 2 = 120. Stopping at n > 1 skips the pointless multiply by 1.

Q3 Write factorial recursively in Python and explain the base case.
Python
def fact(n):
    if n <= 1:
        return 1
    return n * fact(n - 1)

print(fact(5))

The base case n <= 1 returns 1 without calling itself again. Without it the calls would never stop. In a flowchart, the call fact(n - 1) is drawn as a predefined process box.

Flowchart symbols used and why

SymbolShape to draw on paperWhere it appears hereWhy
TerminatorRounded boxStart and EndOne entry, one exit.
Input/outputParallelogramRead n and Print factData in and the result out.
ProcessRectangleSet fact = 1, i = 1, Set fact = fact * i, Set i = i + 1Each one calculates or updates a value.
DecisionDiamondi <= n?Decides whether another number must be multiplied in.
LoopA decision whose flow line returns to an earlier stepThe arrow from Set i = i + 1 back to i <= n?Makes the multiply step repeat n times.
Predefined processRectangle with double side barsfact(n - 1) in the recursive versionA call to a function defined elsewhere is drawn with double side bars.

In the interactive diagram every step is a labelled card: the small word above each step names its kind (process, decision or data). When you draw this flowchart by hand, use the standard shapes in the table. The full list is on the flowchart symbols page.

Make your own version

Each link opens the editor with a prompt already filled in. Change the wording, then generate your own flowchart.

How can I use this flowchart?

Download the Factorial Flowchart as a PNG or SVG, or open it in the studio to edit your own copy. To draw a similar flowchart from a description, use the flowchart generator; for notation and a step-by-step walkthrough, read how to make a flowchart; and browse more flowchart examples for related layouts.

Try the studio with your own description.

Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.