思路:flag比较重要,假如没有flag,就需要用break退出循环。
#include<iostream>
using namespace std;
int main()
{
int a;
int prime(int x);
cout<<"please enter a:";
cin>>a;
prime(a);
return 0;
}
int prime(int x)
{
int n;
int flag=0;
if(x<4)
{
cout<<"a is prime"<<endl;
}
else
{
for(n=2;n<x/2+1;n++)
{
if(x%n==0)
{
flag=1;
}
}
if(flag==1)
{
cout<<"a is not prime"<<endl;
}
else
{
cout<<"a is prime"<<endl;
}
}
return 0;
}