This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

7 Aug 2012

C program to print the Prime Numbers Between The two Numbers / Given range

/* c program to print prime numbers between given range */
#include<stdio.h>
//#include<conio.h>
int main()
{

  int i,j,count,min,max;
  //clrscr();
  printf(" Enter min range: ");
  scanf("%d",&min);
  printf(" Enter max range: ");
  scanf("%d",&max);
  if(min==0) //since zero is not a prime number
    min=min+1;
  printf("\n The Prime Numbers Between %d And %d 

           are: \n",min,max);
  for(i = min;i<=max;i++)
  {
    count = 0;
    for(j=2;j<=i/2;j++)
    {
      if(i%j==0)
      {
       count++;
       break; //to come out of if loop..
      }
    }
    if(count==0 && i!= 1 ) //since 1 is not a prime number
     printf(" %d",i);
  }
   printf("\n\n");
   //getch();
   return (0);

}

Output: ( using GNU GCC Compiler with code::blocks IDE, hence no need of clrscr(); and getch(); ) 


1 comment:

Thanks for your comments.
-Sameer