Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<math.h>
- using namespace std;
- int main()
- {
- // Khai bao bien
- int n;
- // Nhap gia tri dau vao
- cout << "Nhap mot so :";
- cin >> n;
- cout << "Danh sach cac so nguyen to nho hon " << n << " la " << endl;
- //vong lap for de tim so nguyen to
- // Khai bao i=2; Voi dieu kien i < n thi tang gia tri cua i len 1
- for (int i = 2; i<n; i++)
- {
- // Tao mot bien boolean prime va gan gia tri la true
- bool prime = true;
- // Khai bao bien j=2, voi j binh phuong nho hon hoac bang i thi tang gia tri cua j len 1
- for (int j = 2; j*j <= i; j++)
- {
- // Neu i chia het cho j thi gan prime gia tri flase va ket thuc chuong trinh
- if (i % j == 0)
- {
- prime = false;
- break;
- }
- }
- // Neu prime la true thi in i ra man hinh console
- if (prime) cout << i << endl;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement