Advertisement
adrienbrody2011

IsPrime function

Aug 19th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool IsPrime(int n) {
  6.     int divisors_counter = 0;
  7.     for (int divisor = 1; divisor <= n; divisor++) {
  8.         if (n % divisor == 0) {
  9.             divisors_counter++;
  10.         }
  11.     }
  12.     return divisors_counter == 2;
  13. }
  14.  
  15. int main() {
  16.     int n;
  17.     cin >> n;
  18.     if (isPrime(n)) {
  19.         cout << "YES\n";
  20.     } else {
  21.         cout << "NO\n";
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement