C++ Prime Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Prime Number?

A prime number is an integer greater than 1 that has exactly two positive divisors: 1 and itself.

Remember
Prime:     2, 3, 5, 7, 11, 13, 17, 19, ...
Not prime: 1 (neither), 4, 6, 8, 9, 10, 12, ...

Rule:
  n > 1 and no divisor in 2 .. floor(√n)
nDivisorsPrime?
71, 7Yes
91, 3, 9No
21, 2Yes (only even prime)
11No

How to Check a Prime Number in C++

Four clear steps — then reuse the helper to list primes in a range.

  1. If n ≤ 1, return false.
  2. If n is 2 or 3, return true (optional fast path).
  3. Try divisors from 2 to √n; if any divides n, it is not prime.
  4. If no divisor is found, n is prime.
MethodIdeaTimeBest for
Trial to √nNo divisor in 2..√nO(√n)Single check (default)
Range scanCall isPrime for each valueO(N√N)Listing primes 1..N

Pseudocode

Pseudocode
function IsPrime(n):
    if n <= 1: return false

    i = 2
    while i * i <= n:
        if n % i == 0:
            return false
        i = i + 1

    return true

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn > 1 with only divisors 1 and n
1Neither prime nor composite
2Smallest and only even prime
Loop boundi * i <= n (same as i <= √n)
CompositeHas a factor ≤ √n
Classic checks7 yes; 9 no; 1 no

Live Preview

Enter an integer to see trial division up to √n and the prime verdict.

Whole numbers only. Tap a chip or type a value.

Result
n = 17
Check divisors from 2 to floor(√17)=4
No divisor found
17 is prime

Worked Walkthrough — n = 17

Test every candidate divisor up to √17.

Calculation
√17 ≈ 4.123 → check i = 2, 3, 4

17 % 2 = 1
17 % 3 = 2
17 % 4 = 1

No divisor → 17 is prime

Counterexample n = 15:
√15 ≈ 3.87 → check i = 2, 3
15 % 3 == 0 → not prime

C++ Programs

Start with a reusable isPrime helper. Then print every prime in a small range with the same helper.

Example 1 — isPrime Check

Return true only when no divisor exists from 2 through √n.

C++
#include <iostream>
using namespace std;

bool isPrime(int n)
{
    if (n <= 1)
    {
        return false;
    }

    for (long long i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

int main()
{
    int number = 17;
    cout << number
         << (isPrime(number) ? " is prime" : " is not prime")
         << endl;
    return 0;
}

How It Works

  1. n <= 1 is rejected — primes start at 2.
  2. The loop runs while i * i <= n, which means i ≤ √n.
  3. Use long long for i so i * i does not overflow for larger n.
  4. If any i divides n, a third divisor exists → not prime.
  5. For 17, checks stop at 4 and none divide, so the answer is true.

Example 2 — Primes in a Range

Reuse isPrime to print every prime from 1 to 20.

C++
#include <iostream>
using namespace std;

bool isPrime(int n)
{
    if (n <= 1)
    {
        return false;
    }

    for (long long i = 2; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            return false;
        }
    }

    return true;
}

int main()
{
    int start = 1;
    int end = 20;

    cout << "Primes from " << start << " to " << end << ":" << endl;
    for (int i = start; i <= end; i++)
    {
        if (isPrime(i))
        {
            cout << i << " ";
        }
    }
    cout << endl;
    return 0;
}

How It Works

  1. Same isPrime helper as Example 1 keeps the logic in one place.
  2. The outer loop visits every integer from start to end.
  3. Only values that pass isPrime are printed.
  4. 1 is skipped automatically by the helper.
  5. For larger ranges, a sieve is faster — but this pattern is perfect for beginners.

Edge Cases & Pitfalls

Check these before calling the solution done.

1

Not prime

Return false for n ≤ 1 before looping.

2

Even but prime

Do not reject all even numbers — 2 is prime.

Bound

Use i * i ≤ n

Avoid floating sqrt in the loop condition when possible.

Negatives

Not prime here

This tutorial defines primes on positive integers only.

Time and Space Complexity

ProgramTimeExtra space
isPrime (√n trial)O(√n)O(1)
Primes in range 1..NO(N√N)O(1)

For large ranges, the Sieve of Eratosthenes is faster. For one number or a short range, √n trial division is enough.

Key Takeaways

  • Definition: n > 1 with only divisors 1 and n.
  • Check: no divisor from 2 to √n.
  • Facts: 1 is not prime; 2 is the only even prime.
  • Test set: 17 → yes; 15 → no; 1 → no; 2 → yes.

One line: if n > 1 and nothing from 2 to √n divides it, n is prime.

Frequently Asked Questions

A prime number is an integer greater than 1 with exactly two positive divisors: 1 and itself. Examples: 2, 3, 5, 7, 11.
No. 1 has only one positive divisor, so it is neither prime nor composite.
If n has a factor larger than √n, the matching factor is already smaller than √n and would have been found earlier.
Yes. 2 is the only even prime.
A single isPrime check with trial division up to √n is O(√n). Listing primes from 1 to N by calling isPrime on each value is about O(N√N).

Next: Pronic Number

Check whether a number equals k × (k + 1).

Pronic number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful