Advertisement
Realratnadwip

Prime Number[Range]

May 21st, 2025 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. void main()
  7. {
  8.     int i, j, up, down, num, count, flag;
  9.    
  10.     //flag = 0 non-prime, flag = 1 prime
  11.     system("cls");
  12.     printf("This is a simple program to print Prime Number.\n\n");
  13.     printf("Enter The upper range : ");
  14.     scanf("%d",&up);
  15.     printf("Enter The lower range : ");
  16.     scanf("%d", &down);
  17.     system("cls");
  18.     printf("Prime-Numbers in ascending order.\n\n");
  19.  
  20.     count = 0;
  21.    
  22.     for (i = down; i <= up; i++)
  23.     {
  24.         num = i;
  25.         flag = 1;
  26.         if(num <= 1)
  27.         {
  28.             flag = 0;
  29.         }
  30.  
  31.         for(j = 2; j <= num / 2; j++)
  32.         {
  33.             if(num % j == 0)
  34.             {
  35.                 flag = 0;
  36.                 break;
  37.             }
  38.         }
  39.  
  40.         if(flag == 1)
  41.         {
  42.             printf("%d - is a Prime Number.\n", num);
  43.             count++;
  44.         }
  45.     }
  46.  
  47.     if (count == 0)
  48.     {
  49.         system("cls");
  50.         printf("There is no Prime-Number in this range.");
  51.     }
  52.     else
  53.     {
  54.         printf("\nThere is total '%d' Prime-Number in this range.", count);
  55.     }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement