Flowchart Updated 12/2/2026

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.

prime numberflowchartloopsalgorithmsprogramming
10 nodes 12 connections

Study guide

Prime Number FlowchartStartRead nn < 2?Set i = 2i * i <= n?n % i == 0?Set i = i + 1Print "n is prime"Print "n is not prime"EndYesNoYesNoYesNo
Prime Number Flowchart. Source: https://www.chartcraft.dev/examples/prime-number-flowchart

What the prime number check flowchart shows

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.

Prime number check flowchart, step by step

  1. terminator StartOne Start terminator opens the flowchart.
  2. input/output Read nThe program reads the number to test and stores it in n. Input goes in a parallelogram.
  3. decision n < 2?0, 1 and negative numbers are never prime. Catching them first keeps the loop simple. Yes jumps straight to the not prime message.
  4. process Set i = 2The first divisor worth trying is 2. Every number is divisible by 1, so 1 proves nothing.
  5. decision i * i <= n?The loop condition. While 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.
  6. decision n % i == 0?The remainder test. If n % i is 0, then i divides n exactly and the search is over: Yes means not prime.
  7. process Set i = i + 1No luck with this i, so move on to the next candidate and go back up to the loop condition.
  8. input/output Print "n is prime"Reached only when the loop ran out of candidates without finding a divisor.
  9. input/output Print "n is not prime"Reached from two places: a number below 2, or a divisor found inside the loop. Two arrows may enter one box.
  10. terminator EndBoth messages flow into the same End terminator.

Dry run: trace table for n = 29

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.

ii * ii * i <= n?n % iDivides?Next step
24Yes1NoSet i = 3
39Yes2NoSet i = 4
416Yes1NoSet i = 5
525Yes4NoSet i = 6
636Nonot testednot testedPrint "29 is prime"

Only four remainders were needed. Testing every number from 2 to 28 would have taken 27.

Prime number check pseudocode

Pseudocode
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

Prime number check in Python, C and Java

Python
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")
C
#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;
}
Java
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");
        }
    }
}
Input typed at the keyboard
29
Output of all three programs
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.

Mistakes examiners see most

  • Calling 1 a prime number. 1 has only one divisor, so it is not prime. Without the n < 2? diamond, the loop never runs for 1 and the flowchart wrongly prints prime.
  • Printing prime inside the loop. One failed division proves nothing. Prime can only be printed after the loop ends with no divisor found, so the prime box hangs off the No exit of the loop condition.
  • Stopping the loop too early. The condition is i * i <= n, not i * i < n. With <, the number 25 would never be tested against 5 and would be called prime.
  • Forgetting the update step. If the No exit of n % i == 0? goes back to the diamond without i = i + 1, the same divisor is tested forever. Compare the while loop flowchart.

Practice questions with worked answers

Q1 Trace the flowchart for n = 91. Which value of i ends the loop, and what is printed?

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.

Q2 Why is it enough to test divisors up to the square root of n?

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.

Q3 Change the flowchart so it tests only 2 and then odd divisors 3, 5, 7 and so on. What boxes change?

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.

Pseudocode
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

Flowchart symbols used and why

SymbolShape to draw on paperWhere it appears hereWhy
TerminatorRounded boxStart and EndOne way in and one way out of the flowchart.
Input/outputParallelogramRead n and both Print boxesThese steps move data into or out of the program.
ProcessRectangleSet i = 2 and Set i = i + 1They change a stored value and have one exit each.
DecisionDiamondn < 2?, i * i <= n? and n % i == 0?Three yes-or-no questions, each with two labelled exits.
LoopA decision whose flow line returns to an earlier stepThe arrow from Set i = i + 1 back up to i * i <= n?Returning to an earlier diamond repeats the divisor test.
Flow lineArrowEvery arrowArrows 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.

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 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.

Try the studio with your own description.

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