#include "stdio.h"
int prime(int n){//素数判断
int i;
if(n>2 && !(n&1) || n<2)
return 0;
for(i=3;i*i<=n;i+=2)
if(!(n%i))
return 0;
return 1;
}
int main(int argc,char *argv[]){
int n,s,i;
printf("Please enter n(int 0if(scanf("%d",&n)!=1 || n<1){
printf("Input error, exit...\n");
return 0;
}
for(s=0,i=2;i<=n;i++)
if(prime(i)){
printf("%d ",i);
s++;
}
printf("\nA total of %d prime.\n",s);
return 0;
}
运行样例: