用c语言编程输出200之间的所有素数,每行输出5个
时间: 2024-05-05 13:19:33 浏览: 16
好的,请稍等片刻,我为您生成代码:
```
#include <stdio.h>
int isPrime(int n) {
if(n < 2) {
return 0;
}
for(int i = 2; i*i <= n; i++) {
if(n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int cnt = 0;
for(int i = 2; i <= 200; i++) {
if(isPrime(i)) {
printf("%d ", i);
cnt++;
if(cnt == 5) {
cnt = 0;
printf("\n");
}
}
}
return 0;
}
```
以上便是用C语言编写的输出200之间的所有素数,并且每行输出5个的程序代码。希望能够对您有所帮助!
相关问题
用c语言编程实现每5个数据一行方式输出101~200之间的所有素数
```c
#include <stdio.h>
int is_prime(int n){
if(n==1) return 0;
for(int i=2;i*i<=n;i++){
if(n%i==0) return 0;
}
return 1;
}
int main(){
int cnt = 0;
for(int i=101;i<=200;i++){
if(is_prime(i)){
printf("%d ", i);
cnt++;
if(cnt%5==0) printf("\n");
}
}
return 0;
}
```
C语言编程输出100以内所有素数
以下是C语言编程输出100以内所有素数的代码:
```c
#include <stdio.h>
int main() {
int flag = 1; // 标记:是素数 flag=1,不是素数 flag=0
// i:从 2 到 99 中找素数
for (int i = 2; i < 100; i++) {
for (int j = 2; j < i; j++) // 让 i 除以 2 到 i-1 之间的每一个数,看能否被整除
{
if (i % j == 0) // 若被整除,则 i 为合数
{
flag = 0; // 跳出,检查下一个 i
break;
}
}
if (flag == 1) // 若flag=1,说明没出现过 i%j = 0 的情况,i为质数,则输出i
{
printf("%d ", i);
}
else // 出现了i%j = 0 的情况,flag=0,i为为合数,不输出,将flag变回1
{
flag = 1;
}
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)