Read the question, write C on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Write int is_prime(int n) that returns 1 if n is prime, else 0.
main already reads n and prints yes or no. 1 is not prime. 2 is.
Input. One integer n ≥ 1.
Output. yes or no.
Constraints
- 1 ≤ n ≤ 10 000
Examples
Input
7
Output
yes
Input
1
Output
no
Input
9
Output
no
Hint
- Return 0 immediately when n < 2.
- Trial-divide from 2 while d * d <= n.
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <stdio.h>
int is_prime(int n) {
if (n < 2) return 0;
for (int d = 2; d * d <= n; d++) {
if (n % d == 0) return 0;
}
return 1;
}
int main(void) {
int n;
if (scanf("%d", &n) == 1) {
printf(is_prime(n) ? "yes\n" : "no\n");
}
return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.