用C语言求1-100素数

2024-11-07 20:55:40
推荐回答(5个)
回答(1):

include
main(){
int i1,i2;
for(i1=3;i1<98;i1++){
for(i2=2;i2 if(i1%i2!=0){
printf("%d ",i1);
}
}
}
}

回答(2):

#include
#include
main(){

int n = 100;
int i,j,m;
int flg = 0;

printf("素数如下:\n");

for(i=2; i<=n; i++)
{
flg = 1;
j = 2;
m = sqrt(i);
while(j {
if(i%j == 0)
flg = 0;
j++;
}

if(flg == 1)
printf(" %d |", i);

}
}

回答(3):

#include

int main ( void )
{
int i, j, p, prime [ 100 ];

p = 0;
prime [ p ++ ] = 2;
for ( i = 2; i < 101; i ++ )
{
for ( j = 0; j < p; j ++ )
if ( !(i % prime [ j ]) ) break;
if ( j >= p )
prime [ p ++ ] = i;
}

for ( i = 0; i < p; i ++ )
printf ( "%d ", prime [ i ] );

return 0;
}

回答(4):

#include "stdio.h"
#include "stdlib.h"
void main(){
int i1,i2;
for(i1=3;i1<98;i1++){
int bFlag = 0;
for(i2=2;i2 if(i1%i2==0){
bFlag = 1;
}
}
if(bFlag == 0) printf("%d\n",i1);
}
}

回答(5):

#include
main()
{
int m,k,i,n=0;
for(m=1;m<100;m+=2)
{
k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0)break;
if(i>=k+1)
{ printf("%d ",m);
n++;
}
}
}