#include<iostream>
using namespace std;
int main(){
long int i,n;
while (cin >> i){
if (i==1){
cout << "非質數" << endl;
}
else if (i==2){
cout << "質數" << endl;
}
else{
for (n=2;n<=i-1;n++){
if (i%n==0){
cout << "非質數" << endl;
break;
}
else if (i%n!=0 && n==i-1){
cout << "質數" << endl;
}
}
}
}
return 0;
}
順便請各位幫我看看有哪裡錯~謝謝!
#include<iostream> using namespace std; int main(){ long int i,n; while (cin >> i){ if (i==1){ cout << "非質數" << endl; } else if (i==2){ cout << "質數" << endl; } else{ for (n=2;n<=i-1;n++){ if (i%n==0){ cout << "非質數" << endl; break; } else if (i%n!=0 && n==i-1){ cout << "質數" << endl; } } } } return 0;}
順便請各位幫我看看有哪裡錯~謝謝!