3-9
编写函数判别一个数是否是质数,在主程序中实现输入输出。
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 using namespace std; 5 6 bool fun(int a){ 7 int b = sqrt(a); 8 for(int i=2; i<=b; i++){ 9 if(a % i == 0) return false; 10 } 11 return true; 12 } 13 int main(){ 14 int a; 15 cout<<"请输入一个数"<<endl; 16 cin>>a; 17 if(fun(a)) cout<<"是质数"<<endl; 18 else cout<<"不是质数"<<endl; 19 return 0; 20 }

浙公网安备 33010602011771号