Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Primes in Range in C++

Print every prime number between two limits in C++ using a reusable isPrime function. Shows how a helper function keeps the loop short and when a sieve is better.

IntermediateLoop ProgramsExample 9 of 14
primes-in-range.cpp
1#include <iostream>
2#include <cmath>
3using namespace std;
4
5bool isPrime(int n) {
6 if (n <= 1) return false;
7 for (int i = 2; i <= sqrt(n); i++) {
8 if (n % i == 0) return false;
9 }
10 return true;
11}
12
13int main() {
14 int start, end;
15
16 cout << "Enter start of range: ";
17 cin >> start;
18
19 cout << "Enter end of range: ";
20 cin >> end;
21
22 cout << "Prime numbers between " << start << " and " << end << " are: ";
23
24 for (int i = start; i <= end; i++) {
25 if (isPrime(i)) {
26 cout << i << " ";
27 }
28 }
29 cout << endl;
30
31 return 0;
32}

Sample Output

Prime numbers between 10 and 30 are: 11 13 17 19 23 29

Each prime from the range, separated by spaces.

In this one

  • How to move a test into its own function
  • How return false exits early from a loop
  • How to loop over a range and print matches
  • When the Sieve of Eratosthenes is faster

How it works

  1. 1

    Read start (10) and end (30).

  2. 2

    Print the label.

  3. 3

    For each i from 10 to 30, call isPrime(i).

  4. 4

    isPrime returns false on the first divisor, true if none.

  5. 5

    Print each prime followed by a space.

Edge cases

start greater than end

Enter 30 then 10.

The loop never runs, so only the label prints with an empty list.

Range includes 0 or 1

Enter 0 to 10.

isPrime rejects n <= 1, so the output starts at 2: 2 3 5 7.

What's going on

This C++ program lists every prime from start to end. 10 to 30: 11 13 17 19 23 29.

bool isPrime(int n) is the same test as Prime Check, pulled into a function so the range loop stays readable.

if (n <= 1) return false;

for (int i = 2; i <= sqrt(n); i++) if n % i == 0 return false.

Otherwise true.

cin >> start; cin >> end;

Sample 10 and 30.

for (int i = start; i <= end; i++)

if (isPrime(i)) cout << i << " ";

10 is not prime (2×5). 11 is. The line ends with a space before endl. Harmless.

start > end: the loop never runs. You still print the "are:" label and a blank list.

A sieve is faster for huge ranges. For 10–30 this is fine. Don't copy-paste the prime test into the loop body — that's how the <= 1 check gets forgotten on the second copy.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

How many primes are between 10 and 20?

Pick an answer to check it.

Question 2

What does return false inside the loop do?

Pick an answer to check it.

Questions people ask

Why write a separate function?

It gives the test a name, keeps main readable, and lets you reuse it without copying the code.

How slow is this for big ranges?

Each number costs up to √n checks. For millions of numbers, a sieve that crosses out multiples is much faster.

Can I name a variable end?

Yes here, but std::end exists, so with using namespace std it can clash in some code. A name like last avoids confusion.