C program to generate and print prime numbers in a given range. Also print the number of prime numbers

by Nideesh C on April 12, 2011 · 0 comments

in C Programming




/* C program to generate and print prime numbers in a given range. Also print the number of prime numbers */

#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);
}
/*———————————
Output
Enter the value of M and N
15 45
Prime numbers are
17
19
23
29
31
37
41
43
Number of primes between 15  and 45 = 8
——————————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to check whether a given number is prime or not and output the given number with suitable message
  2. C program to generate and print first N FIBONACCI numbers
  3. Sample – check if a number is prime
  4. C program to reverse a given integer number and check whether it is a palindrome. Output the given numbers with suitable message
  5. C program to find the sum of odd numbers and sum of even numbers from 1 to N. Output the computed sums on two different lines with suitable headings

Leave a Comment

Previous post:

Next post: