Advertisement
MeehoweCK

Untitled

Nov 19th, 2022
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int x;
  8.     cout << "Podaj liczbe naturalna: ";
  9.     cin >> x;
  10.     if(x < 2)
  11.         cout << x << " nie jest liczba pierwsza.\n";
  12.  
  13.     bool pierwsza = true;
  14.     for(int i = 2; i * i <= x; ++i)
  15.     {
  16.         if(x % i == 0)
  17.         {
  18.             pierwsza = false;
  19.             break;              // natychmiastowe wyjście z pętli
  20.         }
  21.     }
  22.  
  23.     if(pierwsza)
  24.         cout << x << " jest liczba pierwsza.\n";
  25.     else
  26.         cout << x << " nie jest liczba pierwsza.\n";
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement