Prime Check in C++
Check whether a number is prime in C++ by testing divisors up to its square root. Explains why √n is enough, why 0 and 1 are not prime, and how break saves time.
1#include <iostream>2#include <cmath>3using namespace std;45int main() {6 int num;7 bool isPrime = true;89 cout << "Enter a number: ";10 cin >> num;1112 if (num <= 1) {13 isPrime = false;14 } else {15 for (int i = 2; i <= sqrt(num); i++) {16 if (num % i == 0) {17 isPrime = false;18 break;19 }20 }21 }2223 if (isPrime) {24 cout << num << " is a prime number" << endl;25 } else {26 cout << num << " is not a prime number" << endl;27 }2829 return 0;30}
Sample Output
17 is a prime number
No divisor between 2 and 4 was found.
In this one
- The definition of a prime number
- Why checking up to sqrt(n) is enough
- How a bool flag and break work together
- Why 0, 1, and negatives are handled first
How it works
- 1
Read num (17); assume isPrime = true.
- 2
If num <= 1, set isPrime to false.
- 3
Otherwise try i = 2, 3, 4 while i <= sqrt(17) ≈ 4.12.
- 4
None divide 17, so the flag stays true.
- 5
Print the result.
Edge cases
Perfect square
Enter 49.
sqrt(49) is 7, and the loop includes 7 because of <=. 49 % 7 == 0 marks it not prime. Using < would wrongly call 49 prime.
Input 1
Enter 1.
Caught by num <= 1, so 1 is reported as not prime, which matches the definition.
What's going on
A prime number is an integer greater than 1 whose only positive divisors are 1 and itself. 17 is prime. 1 is not. 9 is not (3×3). 2 is the only even prime.
#include <cmath>
For sqrt. The loop only needs to test up to the square root.
int num;
bool isPrime = true;
Assume prime until a divisor shows up. Start false and you'll mark every prime as composite unless you remember to flip it.
cin >> num;
Sample: 17.
if (num <= 1)
isPrime = false;
1 has one divisor. 0 and negatives aren't primes. Don't send them into the loop.
else { for (int i = 2; i <= sqrt(num); i++)
If num % i == 0, you found a factor. Set isPrime = false and break. No point keeping going.
Why sqrt: if n = a × b and a ≤ b, then a ≤ √n. A factor bigger than √n already had a partner you would have seen. Checking through n-1 works and wastes time. 17's loop tests 2, 3, 4 (√17 ≈ 4.1). None divide 17.
Walk 9: √9 is 3. i = 2, 9 % 2 ≠ 0. i = 3, 9 % 3 == 0 → not prime.
Walk 4: i = 2, 4 % 2 == 0 immediately.
Walk 2: sqrt(2) ≈ 1.41, so i <= 1.41 never runs with i starting at 2. isPrime stays true. Correct.
Then print " is a prime number" or " is not". Sample: 17 is a prime number.
Even numbers after 2 are never prime. You could skip them after testing 2. This program doesn't; sqrt already keeps it cheap for homework sizes.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
For num = 25, which i makes the loop break?
Pick an answer to check it.
Question 2
Which value is not prime?
Pick an answer to check it.
Questions people ask
Why stop at the square root?
If n = a × b and both a and b were bigger than √n, their product would be bigger than n. So one factor is always ≤ √n.
Is calling sqrt in the loop condition slow?
It is recomputed every pass. Writing i * i <= num avoids sqrt and floating point entirely.
Is 2 prime?
Yes, the only even prime. The loop does not run for 2, so the flag stays true.