Advertisement
anhkiet2507

Code xuất số nguyên tố

Sep 7th, 2018
37,906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<math.h>    
  4.  
  5. using namespace std;
  6. int main()
  7. {
  8.  
  9.     // Khai bao bien
  10.     int n;
  11.  
  12.     // Nhap gia tri dau vao
  13.     cout << "Nhap mot so :";
  14.     cin >> n;
  15.  
  16.     cout << "Danh sach cac so nguyen to nho hon " << n << " la " << endl;
  17.  
  18.     //vong lap for de tim so nguyen to
  19.         // Khai bao i=2; Voi dieu kien i < n thi tang gia tri cua i len 1
  20.     for (int i = 2; i<n; i++)
  21.     {
  22.         // Tao mot bien boolean prime va gan gia tri la true
  23.         bool prime = true;
  24.                 // Khai bao bien j=2, voi j binh phuong nho hon hoac bang i thi tang gia tri cua j len 1
  25.         for (int j = 2; j*j <= i; j++)
  26.         {
  27.                 // Neu i chia het cho j thi gan prime gia tri flase va ket thuc chuong trinh
  28.             if (i % j == 0)
  29.             {
  30.                 prime = false;
  31.                 break;
  32.             }
  33.         }
  34.                 // Neu prime la true thi in i ra man hinh console
  35.         if (prime) cout << i << endl;
  36.     }
  37.  
  38.     system("pause");
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement