用C语言编写代码输出100以内的质数
时间: 2023-09-11 13:06:30 浏览: 106
以下是用C语言编写的输出100以内质数的代码:
```c
#include <stdio.h>
int main() {
int i, j, is_prime;
printf("Prime numbers between 1 and 100:\n");
// Check every number between 2 and 100
for (i = 2; i <= 100; i++) {
is_prime = 1; // Assume the number is prime
// Check if the number is divisible by any number between 2 and itself
for (j = 2; j < i; j++) {
if (i % j == 0) {
is_prime = 0; // Number is not prime
break;
}
}
// If the number is prime, print it
if (is_prime) {
printf("%d ", i);
}
}
return 0;
}
```
运行以上代码,会输出如下内容:
```
Prime numbers between 1 and 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
```
阅读全文