我剛開始編程。我寫了一個簡單的C程序來檢查素數,但它不起作用。請幫我找出我的錯誤。爲什麼這個程序檢查素數不起作用?
#include<stdio.h>
void main()
{
int n,i,c=0;
printf("enter any number: ");
scanf("%d", &n);
for(i=2;i<n;i++)
{
if(n%i==0)
c=c+1;
if(c>0){
break;
printf("\n%d is not a prime number",n);
}
}
if(i==n)
printf("\n%d is a prime number",n);
}
您的編譯器不會在'break;'後發出語句警告嗎? – 2015-02-06 18:41:26
不知道爲什麼它不工作,但在這裏看起來像一個無用的變量。爲什麼不這樣做:if(n%i == 0) {stuff in here} – 2015-02-06 18:42:20