Task: Read an integer ≥2 and check divisibility up to √n with early exit; report prime/composite. Handle n < 2 clearly.
/*
Program: Prime Number Test with Early Exit (U2)
What it does:
- Reads an integer n.
- If n < 2 -> not prime. Else test divisors from 2 to floor(sqrt(n)) and break early on first factor.
*/
#include <stdio.h>
int main(void) {
int n;
printf("Enter an integer >= 2: ");
if (scanf("%d", &n) != 1) {
printf("Invalid input.
");
return 0;
}
if (n < 2) {
printf("%d is not prime (needs to be >= 2).
", n);
return 0;
}
int isComposite = 0;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) { isComposite = 1; break; }
}
if (!isComposite) printf("%d is prime.
", n);
else printf("%d is composite.
", n);
return 0;
}