#include<stdio.h>
#include<math.h>//用到了sqrt()
int prime(int m)
{ int i,j=1;//j=1先假设m是素数
for(i=2;i<=sqrt(m);i++)
if(m%i==0&&m!=2)//2是素数
{j=0;
break;
}
if(m==1)//1不是素数
j=0;
return j;
}
void main()
{ int M,N,i,j=0,count=0;
printf("Input M N: ");
scanf("%d%d",&M,&N);
while(M>N)
{printf("Error! ");
printf("Input M N again: ");
scanf("%d%d",&M,&N);
}//M>N,重新输入
for(i=M;i<=N;i++)
if(prime(i))
{
count++;
j+=i;
}
printf("The number of prime: %d\n",count);
printf("The sum of prime: %d\n",j);
}
温馨提示:答案为网友推荐,仅供参考