AlexandruT

Verificarea daca un numar este prim

Dec 9th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #define wtrue "Numarul este prim."
  3. #define wfalse "Numarul nu este prim."
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     int n, i;
  10.     cin >> n;
  11.     if(n <= 1) cout << wfalse;
  12.     else if(n == 2) cout << wtrue;
  13.     else if(n % 2 == 0) cout << wfalse;
  14.     else
  15.     {
  16.         bool prim = true;
  17.         for(i = 3; i * i <= n && prim == true; i += 2)
  18.             if(n % i == 0) prim = false;
  19.         if(prim == true) cout << wtrue;
  20.         else cout << wfalse;
  21.     }
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment