C++的:
#include <iostream>
using namespace std;
int main (){
int x,i ;
while (cin>>x){
for (i=2;i<=x;i++){
if (x%i==0)break;
}
if (x!=i){cout << "非質數"<<endl;}
else {cout << "質數"<<endl;}
}
return 0;
}
C++的:
#include
using namespace std;
int main (){
int x,i ;
while (cin>>x){
for (i=2;i<=x;i++){
if (x%i==0)break;
}
if (x!=i){cout << "非質數"<
else {cout << "質數"<
}
return 0;
}
加入紅色部分
C++的:
#include <iostream>
#include <cmath>
using namespace std;
int main (){
int x,i ;
while (cin>>x){
for (i=2;i<=sqrt(x);i++){
if (x%i==0)break;
}
if (x!=i){cout << "非質數"<
else {cout << "質數"<
}
return 0;
}
sqrt 為根號