Advertisement
roneygomes

500 Primeiros Primos

May 11th, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. using namespace std;
  2.  
  3. int main()
  4. {
  5.     bool primo;
  6.  
  7.     // Inicializa o número a ser testado e um contador 'i'
  8.     // que indica a quantidade de primos exibidos.
  9.     for(int num = 2, i = 1; i <= 500; num++)
  10.     {
  11.         for(int j = 2; j < num; j++)
  12.         {
  13.             // Verifica se 'num' é divisível por algum número
  14.             // entre 2 e 'num' - 1.
  15.             if(num%j == 0)
  16.             {
  17.                 primo = false;
  18.                 break;
  19.             }
  20.             else
  21.                 primo = true;
  22.         }
  23.  
  24.         if((primo)||(num == 2))
  25.         {
  26.             cout << "\n" << i << ": " << num;
  27.             i++;
  28.         }
  29.     }
  30.  
  31.    return 0;
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement