Home » Find Prime Number in a Given Range C program

Find Prime Number in a Given Range C program

Learn about Find Prime Number in a Given Range C program in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

Find Prime Number in a Given Range C program

Program:

#include<stdio.h>
int main()
{
  int m, n, i, j, count;

  printf("Enter m and n Values(m<n): ");
  scanf("%d %d",&m,&n);

  if(m == 1) m++;
  printf("Prime numbers from %d to %d are: \n", m, n);

  for(i=m;i<=n;i++)
  {
      count=0;
      for(j=2;j<=i/2;j++)
      {
     if(i%j==0)
     {
       count=1;
       break;
     }
      }

      if(count==0)
     printf("%d\t", i);
  }

  return 0;
}

Output:

Enter m and n Values(m<n): 1 20
Prime numbers from 2 to 20 are:
2 3 5 7 11 13 17 19

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

Similar Codes :
Prime number program in C using while loop
Prime Number Program in C