thespeedracer38

2017 SW Practical SET 1 Q5

Dec 4th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <ctype.h>
  4.  
  5. int isPrime(int);
  6.  
  7. int main(void)
  8. {
  9.     char choice;int ch, i, ctr;
  10.     clrscr();
  11.     do
  12.     {
  13.         ctr = 1;
  14.         printf("\nPress 1 to print prime numbers\n");
  15.         printf("Press 2 to print non-prime numbers\n");
  16.         scanf("%d", &ch);
  17.         switch(ch)
  18.         {
  19.             case 1: printf("The prime numbers are\n");
  20.                 for(i = 2; i <= 2000; i++)
  21.                 {
  22.                     if(isPrime(i))
  23.                     {
  24.                         printf("%d,", i);
  25.                         ctr++;
  26.                     }
  27.                     if((ctr%28) == 0)
  28.                     {
  29.                         printf("\nPress enter to see the next set of numbers....");
  30.                         getch();
  31.                         clrscr();
  32.                         ctr = 1;
  33.                         printf("\n\n");
  34.                     }
  35.                 }
  36.                 break;
  37.             case 2: printf("The non-prime numbers are\n");
  38.                 for(i = 2; i <= 2000; i++)
  39.                 {
  40.                     if(!isPrime(i))
  41.                     {
  42.                         printf("%d,", i);
  43.                         ctr++;
  44.                     }
  45.                     if((ctr%40) == 0)
  46.                     {
  47.                         printf("\nPress enter to see the next set of numbers....");
  48.                         getch();
  49.                         clrscr();
  50.                         ctr = 1;
  51.                         printf("\n\n");
  52.                     }
  53.                 }
  54.                 break;
  55.         }
  56.         printf("\nDo you want to run again (Y/N)? ");
  57.         choice = getche();
  58.     }
  59.     while(toupper(choice) == 'Y');
  60.     getch();
  61.     return 0;
  62. }
  63.  
  64. int isPrime(int num)
  65. {
  66.     int limit = num/2;
  67.     int ret = 1, i;
  68.     for(i = 2; i <=limit; i++)
  69.     {
  70.         if((num%i) == 0)
  71.         {
  72.             ret = 0;
  73.             break;
  74.         }
  75.     }
  76.     return ret;
  77. }
Add Comment
Please, Sign In to add comment