Prime number check
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 < 2?"}
np[/"Print N is not prime"/]
init["i = 2"]
d1{"i × i ≤ N?"}
d2{"N mod i = 0?"}
inc["i = i + 1"]
pr[/"Print N is prime"/]
e(["End"])
s --> in
in --> d0
d0 -->|Yes| np
d0 -->|No| init
init --> d1
d1 -->|Yes| d2
d1 -->|No| pr
d2 -->|Yes| np
d2 -->|No| inc
inc --> d1
pr --> e
np --> ePaste into any Markdown tool that renders Mermaid, such as GitHub.
About this prime number flowchart
A prime number is a whole number greater than 1 whose only divisors are 1 and itself: 2, 3, 5, 7, 11 and so on. The flowchart checks a number N by trying possible divisors. If any divides N exactly, N is not prime.
You only need to try divisors up to the square root of N. If N had a divisor larger than its square root, the matching divisor would be smaller than the square root and would already have been found. The flowchart writes this as i × i ≤ N, which avoids calculating a square root and works with whole numbers only.
The first decision handles the edge cases: 0, 1 and negative numbers are not prime by definition. The loop then has two exits: finding a divisor (not prime) and running out of divisors to try (prime). Both lead to the same end oval.
Step by step
- Input. Read N.
- Edge cases. If N is less than 2, print not prime and end.
- Initialize. Start the divisor at i = 2.
- Loop test. While i × i ≤ N, keep testing. When it fails, no divisor exists, so N is prime.
- Divisor test. If N mod i = 0, i divides N, so N is not prime.
- Next divisor. Otherwise 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 prime number 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 = 29: i goes 2, 3, 4, 5; none divides 29; at i = 6, 36 > 29, so 29 is prime.
- Trace N = 21: i = 2 does not divide; i = 3 does, so 21 is not prime.
- Trace N = 2: 2 × 2 = 4 > 2, so the loop never runs and 2 is prime, which is correct.
- Pseudocode: read N; if N < 2 print not prime and stop; i = 2; while i × i ≤ N: if N mod i = 0 print not prime and stop; i = i + 1; print prime.
- A faster version tests 2 separately and then only odd i; the flowchart gains one decision and halves the work.
Frequently asked questions
- How does the prime number flowchart work?
- It rejects numbers below 2, then tries divisors starting at 2 while i × i ≤ N. If any divisor leaves no remainder, N is not prime. If the loop ends without finding one, N is prime.
- Why check only up to the square root?
- Divisors come in pairs that multiply to N, and one of each pair is at most the square root of N. If none is found up to there, there is none above it either.
- Is 1 a prime number?
- No. A prime has exactly two distinct divisors, and 1 has only one. The first decision handles it.
- How do I print all primes up to a number?
- Put this check inside an outer loop that runs N from 2 to your limit, or use the Sieve of Eratosthenes, which is faster for long ranges.
- Can I open this flowchart in the editor?
- Yes. Press Use this template to open it with a free account, or type your own version into the text to flowchart tool.