Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bài 11: Viết chương trình liệt kê n số nguyên tố đầu tiên.
- CODE 1:
- #include <iostream>
- Viết chương trình nhập số nguyên dương n. Liệt kê n số nguyên tố đầu tiên
- #include <iostream>
- using namespace std;
- bool KiemTraCP(int n)
- {
- int i=1;
- while (i<=n/2)
- {
- if (i*i==n)
- return true;
- i++;
- }
- return false;
- }
- void main ()
- {
- int n;
- cout<<"Nhap so can KT ";
- cin>>n;
- if (KiemTraCP(n))
- cout<<"Day la so Chinh Phuong"<<endl;
- else
- cout<<"Khong la so Chinh Phuong"<<endl;
- }
- bool KiemTraCP(int n)
- {
- int i=1;
- while (i<=n/2)
- {
- if (i*i==n)
- return true;
- i++;
- }
- return false;
- }
- void main ()
- {
- int n;
- cout<<"Nhap so can KT ";
- cin>>n;
- if (KiemTraCP(n))
- cout<<"Day la so Chinh Phuong"<<endl;
- else
- cout<<"Khong la so Chinh Phuong"<<endl;
- }
- CODE 2:
- int i=0,n,j=0;
- printf("Muon hien bao nhieu so nguyen to :");
- scanf("%d",&n);
- while (i<n)
- {
- j+=2;
- if (kiemtranguyento(n)) printf("%d",j),i++;
- }
- }
- CODE 3:
- #include<stdio.h>
- #include<conio.h>
- #include<math.h>
- long ngto(int n)
- {
- if(n==0||n==1) return 0;
- long i;
- for(i=2;i<=sqrt(n);i++)
- if(n%i==0) return 0;
- return 1;
- }
- void lietke()
- { int i=2;
- int n,count=0;
- printf("\n Nhap n=");
- scanf("%d",&n);
- while(count<n)
- {
- if(ngto(i)==1){
- printf("%5d",i);count++;}
- i++;
- }
- }
- void main()
- {
- //clrscr();
- lietke();
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment