质数(prime number)

判断一个数是否是质数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <iostream>
using namespace std;
int main(){
    int n,i=2;
    cout<<"please input a number: "; cin>>n;
    while (n%i!=0 && i<=n-1){
    i++;
    }
   if(i==n)
   cout<<n<<" is a prime number"<<endl;
   else
   cout<<n<<" is not a prime number"<<endl;
return 0;
}

edit & run

Scroll to Top