A classic: which numbers from 101 to 200 are prime, and how many are there? Show each prime on its own line, smallest first, then the count on the last line. For each n, the starter tries dividing from 2 up to the square root. prime starts as 1 (prime) and becomes 0 when a divisor is found.
101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 21
#include <stdio.h>
int main(void) {
int count = 0;
for (int n = 101; n <= 200; n++) {
int prime = 1;
for (int i = 2; i * i <= n; i++) {
// your code here: if n divides by i, it isn't prime, stop trying
}
if (prime) {
// your code here: if it is prime, show n and add 1 to count
}
}
printf("%d\n", count);
return 0;
}
Run tries the examples. When they look right, Submit.