Skip to content
Home » Program to print Prime Numbers in a Range in C

Program to print Prime Numbers in a Range in C

Learn about Program to print Prime Numbers in a Range in C in the below code example.
Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Program to print Prime Numbers in a Range in C

If Prime Numbers should be printed between range then while loop must be iterated range-1 times. For this we have to declare low range and high range. Here is the example of Program to print Prime Numbers in a Range.

#include <stdio.h>
int main() 
{
   int l,h,i,j,set;
   printf("Enter two numbers(intervals): ");
   scanf("%d %d", &l, &h);
   printf("Prime numbers between %d and %d are: ", l,h);
   while(l<h)
   {
       if(l==1)//ignoring if first element is 1
       {
           l++;
           continue;
       }          
      set=0;
      for (i = 2; i <= l / 2; ++i)
       {
           if (l%i==0)
           {
               set=1;
               break;
           }
      }
      if (set==0)
        printf("%d ",l);
    l++;
   }
   return 0;
}

Enter two numbers(intervals): 10
30
Prime numbers between 10 and 30 are: 11 13 17 19 23 29

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Also read:

Prime number in C