Code Logo

Prime Number Checker

Published at19 Apr 2026
Logic & Conditionals Easy 4 views
Like0

Prime numbers are the building blocks of mathematics, and in Prime Number Checker, you will be writing the logic to identify them. A number is "prime" if it is greater than 1 and can only be divided by 1 and itself without leaving a remainder.

Your job is to take an input number and figure out if it fits that definition. Usually, this means checking if any other numbers (like 2, 3, or anything up to half of the number) can divide into it cleanly. If you find even one divisor, the number is not prime.

Take 7 for example; nothing divides it except 1 and 7, so it is Prime. But if you check 10, you will quickly see that 2 and 5 both work, which means 10 is definitely not prime. Your algorithm should be able to give a simple "PRIME" or "NOT PRIME" verdict for any number it receives.

Learn about our pseudocode specification
Guide

Algorithm Flow

Recommendation Algorithm Flow for Prime Number Checker
Recommendation Algorithm Flow for Prime Number Checker

Solution Approach

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The basic approach is to test for divisibility.

is_prime <- true
for i <- 2 to n DIV 2 do
   if n mod i = 0 then
      is_prime <- false
   endif
endfor

We check every integer starting from 2 up to n DIV 2. If n is divisible by any of these numbers (meaning n mod i = 0), then it cannot be prime. For larger numbers, this can be further optimized by checking only up to the square root of n.

Best Answers

Pseudocode - Approach 1
program check_prime
dictionary
   n, i: integer
   is_prime: boolean
algorithm
   input(n)
   if n < 2 then is_prime <- false
   else
      is_prime <- true
      for i <- 2 to n DIV 2 do
         if n mod i = 0 then is_prime <- false endif
      endfor
   endif
   if is_prime then output("PRIME") else output("NOT PRIME") endif
endprogram