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.
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.
Study guide
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.
n whose factorial we want.fact 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.i.n = 0 the loop never runs and this prints 1, which is correct.Each row is one test of i <= n?, including the last one, which fails.
| Test | i | i <= n? | fact = fact * i | i after i + 1 | Next step |
|---|---|---|---|---|---|
| 1 | 1 | Yes | 1 * 1 = 1 | 2 | Back to i <= n? |
| 2 | 2 | Yes | 1 * 2 = 2 | 3 | Back to i <= n? |
| 3 | 3 | Yes | 2 * 3 = 6 | 4 | Back to i <= n? |
| 4 | 4 | Yes | 6 * 4 = 24 | 5 | Back to i <= n? |
| 5 | 5 | Yes | 24 * 5 = 120 | 6 | Back to i <= n? |
| 6 | 6 | No | not run | not run | Print fact (120), then End |
5 multiplications and 6 tests. The loop test always runs once more than the body.
READ n
fact = 1
i = 1
WHILE i <= n
fact = fact * i
i = i + 1
ENDWHILE
PRINT fact
n = int(input())
fact = 1
i = 1
while i <= n:
fact = fact * i
i = i + 1
print(fact)
#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;
}
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);
}
}
5
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.
n itself, so 5 gives 24 instead of 120.long long or long.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.
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.
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.
| Symbol | Shape to draw on paper | Where it appears here | Why |
|---|---|---|---|
| Terminator | Rounded box | Start and End | One entry, one exit. |
| Input/output | Parallelogram | Read n and Print fact | Data in and the result out. |
| Process | Rectangle | Set fact = 1, i = 1, Set fact = fact * i, Set i = i + 1 | Each one calculates or updates a value. |
| Decision | Diamond | i <= n? | Decides whether another number must be multiplied in. |
| Loop | A decision whose flow line returns to an earlier step | The arrow from Set i = i + 1 back to i <= n? | Makes the multiply step repeat n times. |
| Predefined process | Rectangle with double side bars | fact(n - 1) in the recursive version | A 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.
Each link opens the editor with a prompt already filled in. Change the wording, then generate your own 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.
Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.