编程序输出3--100之间的素数

来源:百度知道 编辑:UC知道 时间:2024/05/26 13:03:39

给,已经编译运行确认:
#include<conio.h>
#include<stdio.h>
int main()
{
int count=0;
int s=0;
for(int i=3;i<100;i++)
{
count=0;
for(int j=1;j<=i/2;j++)
{
if(i%j==0)
{
count++;

}
}
if(count==1)
{printf("%5d",i);s++;}
if(s==10)
{printf("\n");s=0;}
}

getch();
return 1;
}

运行结果:
3 5 7 11 13 17 19 23 29 31
37 41 43 47 53 59 61 67 71 73
79 83 89 97

不知道用c编的行不,tc2.0下编译通过。

#include <stdio.h>
#include <math.h>

int main()
{
int i,j;
for(i=3;i<101;i++)
{
for(j=2;j<=sqrt(i);j++)
{
if(i%j==0) break;
}
if(j>sqrt(i)) printf("%d ",i);
}
getch();
return 0