Advertisement
thodantrongdua

liet ke so nguyen to tu 2 den N

Mar 30th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.54 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "conio.h"
  3.  
  4. int so_nguyen_to(int N);
  5. int main() {
  6.   int N, i;
  7.  
  8.   do {
  9.     printf("\n Nhap vao so N = ");
  10.     scanf("%d", & N);
  11.   }
  12.   while (N <= 0);
  13.   printf("\n Cac so nguyen to nho hon %d :", N);
  14.   for (i = 2; i <= N; i++) {
  15.     if (so_nguyen_to(i))
  16.       printf(" %d ", i);
  17.   }
  18.   getch();
  19. }
  20.  
  21. // Ham kiem tra so nguyen to
  22. int so_nguyen_to(int N) {
  23.   int i;
  24.   if (N == 2)
  25.     return 1;
  26.   else {
  27.     for (i = 2; i < N; i++) {
  28.       if (N % i == 0)
  29.         return 0;
  30.     }
  31.     return 1;
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement