break exits the loop immediately. continue skips to the next iteration.
// break: find first multiple of 7
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0) {
cout << "First: " << i;
break; // stop the loop
}
}
// continue: print only odd numbers
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue; // skip evens
cout << i << " "; // 1 3 5 7 9
}
Only check divisors up to √n — if no divisor found by then, n is prime:
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
Implement isPrime(int n) as described above.
Click "Run" to execute your code.