DidiMilikina

10. Check Prime

Oct 21st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. #include <iostream>
  2. #include<math.h>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int number;
  7.     cin >> number;
  8.  
  9.     bool is_prime = true;
  10.     int sqrt_num = sqrt(number);
  11.  
  12.     if (number >= 2)
  13.     {
  14.         for (size_t i = 2; i <= sqrt_num; i++)
  15.         {
  16.             if (number % i == 0)
  17.             {
  18.                 is_prime = false;
  19.                 break;
  20.             }
  21.         }
  22.         if (is_prime)
  23.         {
  24.             cout << "Prime" << endl;
  25.         }
  26.         else {
  27.             cout << "Not prime" << endl;
  28.         }
  29.     }
  30.     else {
  31.         cout << "Not prime" << endl;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment