Flowchart examples · Programming and algorithms

Flowchart to check if a number is prime

Reject numbers below 2, then try divisors from 2 while i × i ≤ N. The first divisor found means not prime; none means prime.

Use this template Describe your own version

Prime number check

Prime number checkYesNoYesNoYesNoStartRead NN < 2?Print N is not primei = 2i × i ≤ N?N mod i = 0?i = i + 1Print N is primeEnd
Use this template →Opens this exact diagram in the editor, every box and arrow editable. Free account; no AI credits used.

A worked example to adapt. Rename steps, add branches and owners to match how your team actually works.

Edit the steps to match your process. The free preview (no sign-up) drafts up to 8 main steps; add branches and the remaining steps in the editor.
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 --> e

Paste 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

  1. Input. Read N.
  2. Edge cases. If N is less than 2, print not prime and end.
  3. Initialize. Start the divisor at i = 2.
  4. Loop test. While i × i ≤ N, keep testing. When it fails, no divisor exists, so N is prime.
  5. Divisor test. If N mod i = 0, i divides N, so N is not prime.
  6. Next divisor. Otherwise add 1 to i and go back to the loop test.

How to make it in flow-chart.io

  1. 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.
  2. 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.
  3. 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.
  4. 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

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.

Related

Factorial flowchartFlowchart to check even or odd numberFlowchart to find the largest of three numbersLeap year flowchartBinary search flowchartFlowchart symbolsHow to make a flowchartText to flowchart toolAll flowchart examples

Build it from your own words

Describe your process and get an editable diagram. Free to build and edit; Flow Pro adds downloads and unlimited projects.

Start free See pricing