Prime Number Flowchart
Flowchart to check prime number: read n, reject anything below 2, then try every divisor i while i * i <= n. With a trace table for n = 29, pseudocode, and tested Python, C and Java code.
Flowchart to check prime number: read n, reject anything below 2, then try every divisor i while i * i <= n. With a trace table for n = 29, pseudocode, and tested Python, C and Java code.
Study guide
A prime number is a whole number greater than 1 whose only divisors are 1 and itself. A flowchart to check prime number tries the possible divisors one by one, and one exact division is enough to prove the number is not prime.
The smart part is where the loop stops: if n has a divisor, it has one no bigger than its square root, so testing i * i <= n saves most of the work.
n. Input goes in a parallelogram.i * i is at most n, there is still a divisor worth trying. When No, every candidate failed, so n is prime. Using i * i <= n avoids square roots and rounding errors.n % i is 0, then i divides n exactly and the search is over: Yes means not prime.i, so move on to the next candidate and go back up to the loop condition.Follow the flowchart with n = 29. 29 is not below 2, so i starts at 2. Each row is one visit to the loop condition.
| i | i * i | i * i <= n? | n % i | Divides? | Next step |
|---|---|---|---|---|---|
| 2 | 4 | Yes | 1 | No | Set i = 3 |
| 3 | 9 | Yes | 2 | No | Set i = 4 |
| 4 | 16 | Yes | 1 | No | Set i = 5 |
| 5 | 25 | Yes | 4 | No | Set i = 6 |
| 6 | 36 | No | not tested | not tested | Print "29 is prime" |
Only four remainders were needed. Testing every number from 2 to 28 would have taken 27.
READ n
IF n < 2 THEN
PRINT n, "is not prime"
ELSE
prime = TRUE
i = 2
WHILE i * i <= n AND prime
IF n MOD i = 0 THEN
prime = FALSE
ENDIF
i = i + 1
ENDWHILE
IF prime THEN
PRINT n, "is prime"
ELSE
PRINT n, "is not prime"
ENDIF
ENDIF
n = int(input())
prime = n >= 2
i = 2
while prime and i * i <= n:
if n % i == 0:
prime = False # found a divisor, stop
i = i + 1
if prime:
print(n, "is prime")
else:
print(n, "is not prime")
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
int prime = n >= 2;
for (int i = 2; prime && i * i <= n; i++) {
if (n % i == 0) {
prime = 0; /* found a divisor, stop */
}
}
if (prime) {
printf("%d is prime\n", n);
} else {
printf("%d is not prime\n", n);
}
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();
boolean prime = n >= 2;
for (int i = 2; prime && i * i <= n; i++) {
if (n % i == 0) {
prime = false; // found a divisor, stop
}
}
if (prime) {
System.out.println(n + " is prime");
} else {
System.out.println(n + " is not prime");
}
}
}
29
29 is prime
All three programs were compiled and run with the input 29 before this page was published, and they print exactly the output shown. They use a flag variable prime so the loop stops as soon as a divisor turns up, which is the Yes exit of the n % i == 0? diamond.
n < 2? diamond, the loop never runs for 1 and the flowchart wrongly prints prime.i * i <= n, not i * i < n. With <, the number 25 would never be tested against 5 and would be called prime.n % i == 0? goes back to the diamond without i = i + 1, the same divisor is tested forever. Compare the while loop flowchart.i = 2, 3, 4, 5 and 6 all leave a remainder (91 % 2 = 1, 91 % 3 = 1, 91 % 4 = 3, 91 % 5 = 1, 91 % 6 = 1).
At i = 7, 7 * 7 = 49 <= 91 and 91 % 7 = 0, because 91 = 7 x 13. The Yes exit is taken and the output is 91 is not prime.
91 looks prime to many people, which is why examiners like it.
Divisors come in pairs that multiply to n, for example 3 x 12 = 36. In each pair, one number is at most the square root and the other is at least the square root.
So if n has any divisor other than 1 and n, the smaller one of its pair is at most the square root. If none is found there, there is none anywhere.
Add a decision n % 2 == 0? after n < 2?. Its Yes exit prints not prime, unless n is 2 itself, so first check n == 2? and send Yes to the prime box.
Then start with Set i = 3 and change the update to Set i = i + 2. The loop now does about half the work.
READ n
IF n < 2 THEN PRINT "not prime"
ELSE IF n = 2 THEN PRINT "prime"
ELSE IF n MOD 2 = 0 THEN PRINT "not prime"
ELSE
i = 3
WHILE i * i <= n AND n MOD i <> 0
i = i + 2
ENDWHILE
IF i * i > n THEN PRINT "prime" ELSE PRINT "not prime"
ENDIF
| Symbol | Shape to draw on paper | Where it appears here | Why |
|---|---|---|---|
| Terminator | Rounded box | Start and End | One way in and one way out of the flowchart. |
| Input/output | Parallelogram | Read n and both Print boxes | These steps move data into or out of the program. |
| Process | Rectangle | Set i = 2 and Set i = i + 1 | They change a stored value and have one exit each. |
| Decision | Diamond | n < 2?, i * i <= n? and n % i == 0? | Three yes-or-no questions, each with two labelled exits. |
| Loop | A decision whose flow line returns to an earlier step | The arrow from Set i = i + 1 back up to i * i <= n? | Returning to an earlier diamond repeats the divisor test. |
| Flow line | Arrow | Every arrow | Arrows give the order of the steps. |
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 Prime Number 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.