用c 写一个输出3到100的素数 每行输出5个素数
时间: 2023-05-28 16:05:19 浏览: 52
```c
#include <stdio.h>
int isPrime(int n) { // 判断是否是素数
if (n < 2) {
return 0;
}
int i;
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int count = 0; // 计数器,控制每行输出5个素数
int i;
for (i = 3; i <= 100; i++) {
if (isPrime(i)) {
printf("%d ", i);
count++;
if (count == 5) { // 每行输出5个素数
printf("\n");
count = 0;
}
}
}
return 0;
}
```
相关问题
用c 写一个输出3到100之间的质数 每5行间隔
#include <stdio.h>
int main()
{
int i, j, count = 0;
for(i=3; i<=100; i++){
for(j=2; j<i; j++){
if(i%j == 0){
count++;
break;
}
}
if(count == 0){
printf("%d\t", i);
if((i-2)%15 == 0)
printf("\n");
}
count = 0;
}
return 0;
}
用c 写一个输出3到100的素数 每5个换行
#include <stdio.h>
int isPrime(int num) {
if (num < 2) {
return 0;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int count = 0;
for (int i = 3; i <= 100; i++) {
if (isPrime(i)) {
printf("%d ", i);
count++;
if (count % 5 == 0) {
printf("\n");
}
}
}
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)