我试图找到素数,似乎我犯了错误,但我自己几个小时都找不到它 这是我的代码:
#include<stdio.h>
#include<math.h>
int main(){
int n,a,b;
printf("enter a number=> \n");
scanf("%d",&n);
for(a=2;a<=n;a++){
for(b==2;b<=a;b++){
if(a%b==0){
break;
}else{printf("Prime number: %d\n",a);}
}
}
return 0;
}
b 应该增加但它没有增加,我也分配了 b==2 但它打印出 b=1
最佳答案
改变
for(b==2;b<=a;b++)
// ^ Replace it with assignment operator '='
到
for(b = 2; b*b <=a; b++)
// ^ Reduce the unnecessary loop iteration.
关于c - 我试图使用 for 循环找到素数 1-n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23272669/