每一个数都会显示为素数
#include
#include
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int a,b,i,j,k,c;
scanf("%d %d",&a,&b);
i = a;
j = 2;
k = 2;
c = 0;
while(a<=i && i<=b){
while(j<=i){
if(i%j==0){
break;
}else{
k++;
j++;
}
}
if(k==i){
c++;
printf("%d是素数 %d个",i,c);
}
i++;
}
return 0;
}
又看你提问了。。。
给你个函数,你把代码改下,如果又问题了,再问我。。。。
下面这个函数,你直接使用,用来判断 参数 n 是否为素数,你那样写不对的
int sushu(int n)
{
if(n<=3)
return n>1;
for(int i = 2; i * i <= n; i++)
if(n%i == 0)
return 0;
return 1;
}
#include<stdio.h>
int main(int argc, char *argv[]) {
int a,b,i,j,c;
scanf("%d %d",&a,&b);
c = 0;
i = a;
while(i<=b){
j = 2;
while(j<i){
if(i%j==0){
break;
}
j++;
}
if(j==i){
c++;
printf("%d是素数\n",i);
}
i++;
}
printf("共有%d个素数\n",c);
return 0;
}