Feb 15, 2012

C program to generate and print prime numbers in a given range.

Write a C program to generate and print prime numbers in a given range. Also print the number of prime numbers.
Prime number is a whole number and greater than 1, which is divisible by one or itself.
Example: 2, 3, 5, 7, 11, 13............ Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

 
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
 int M, N, i, j, flag, temp, count = 0;

 clrscr();

 printf("Enter the value of M and N\n");
 scanf("%d %d", &M,&N);

 if(N < 2)
 {
  printf("There are no primes upto %d\n", N);
  exit(0);
 }
 printf("Prime numbers are\n");
 temp = M;

 if ( M % 2 == 0)
 {
  M++;
 }
 for (i=M; i<=N; i=i+2)
 {
  flag = 0;

  for (j=2; j<=i/2; j++)
  {
   if( (i%j) == 0)
   {
    flag = 1;
    break;
   }
  }
  if(flag == 0)
  {
   printf("%d\n",i);
   count++;
  }
 }
 printf("Number of primes between %d  and %d = %d\n",temp,N,count);
}



Read more Similar C Programs
Learn C Programming

Number System

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!
To browse more C Programs visit this link