Check Prime Number
C++
Medium
2 views
Problem Description
Check if number is prime (only divisible by 1 and itself).
Real Life: Like checking if you can divide chocolates equally in groups.
Step-by-Step Logic:
1. Take number to check
2. Try dividing by all numbers from 2 to number-1
3. If any number divides completely, not prime
4. If no number divides, it's prime
5. Print result
Official Solution
void control_q9_prime_check() {
int number = 29;
bool isPrime = true;
if(number <= 1) {
isPrime = false;
} else {
for(int i = 2; i * i <= number; i++) {
if(number % i == 0) {
isPrime = false;
break; // Found divisor, no need to check more
}
}
}
if(isPrime) {
cout << number << " is a Prime number" << endl;
} else {
cout << number << " is NOT a Prime number" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!