thien

BÀI 11

Mar 2nd, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. bài 11: Viết chương trình liệt kê n số nguyên tố đầu tiên.
  2.  
  3. CODE 1:
  4. #include <iostream>
  5. 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
  6.  
  7. #include <iostream>
  8. using namespace std;
  9. bool KiemTraCP(int n)
  10. {
  11. int i=1;
  12. while (i<=n/2)
  13. {
  14. if (i*i==n)
  15. return true;
  16. i++;
  17. }
  18. return false;
  19. }
  20. void main ()
  21. {
  22. int n;
  23. cout<<"Nhap so can KT ";
  24. cin>>n;
  25. if (KiemTraCP(n))
  26. cout<<"Day la so Chinh Phuong"<<endl;
  27. else
  28. cout<<"Khong la so Chinh Phuong"<<endl;
  29. }
  30. bool KiemTraCP(int n)
  31. {
  32. int i=1;
  33. while (i<=n/2)
  34. {
  35. if (i*i==n)
  36. return true;
  37. i++;
  38. }
  39. return false;
  40. }
  41. void main ()
  42. {
  43. int n;
  44. cout<<"Nhap so can KT ";
  45. cin>>n;
  46. if (KiemTraCP(n))
  47. cout<<"Day la so Chinh Phuong"<<endl;
  48. else
  49. cout<<"Khong la so Chinh Phuong"<<endl;
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. CODE 2:
  57. int i=0,n,j=0;
  58. printf("Muon hien bao nhieu so nguyen to :");
  59. scanf("%d",&n);
  60. while (i<n)
  61. {
  62. j+=2;
  63. if (kiemtranguyento(n)) printf("%d",j),i++;
  64. }
  65.  
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72. CODE 3:
  73. #include<stdio.h>
  74. #include<conio.h>
  75. #include<math.h>
  76.  
  77. long ngto(int n)
  78. {
  79. if(n==0||n==1) return 0;
  80. long i;
  81. for(i=2;i<=sqrt(n);i++)
  82. if(n%i==0) return 0;
  83. return 1;
  84. }
  85. void lietke()
  86. { int i=2;
  87. int n,count=0;
  88. printf("\n Nhap n=");
  89. scanf("%d",&n);
  90. while(count<n)
  91. {
  92. if(ngto(i)==1){
  93. printf("%5d",i);count++;}
  94. i++;
  95. }
  96. }
  97. void main()
  98. {
  99. //clrscr();
  100. lietke();
  101. getch();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment