C Program to print and count all prime numbers in a given range

A number is called a prime number if it has only two divisors which are '1' and that 'number' itself. For a better understanding of this counting all prime number C program, we always recommend you to learn the basic topics of C programming listed below:

What is a prime number?

In this C example, you will learn to print all prime numbers between two numbers (entered by the user) by making a user-defined function. A number that is not divisible by any other numbers except that number and  '1' is called a prime number. This property is called primality, For example, 7 is a prime number because 7 has only two divisors such that '1' and '7' itself.

Note: The largest prime number has '17,425,170' digits.

How to generate prime numbers in C?

This C program needs to generate prime numbers of a particular range, and we need to find out the count of prime numbers displayed.

We are using a little complex logic in this C program as we are using a nested for loop. First, check if the given numbers are greater than 2; if not, the program exits with a message "not a valid number to check for prime."

Now use a nested 'for loop', in the outer loop, check numbers from 'm' to 'n' and set flag = 0, here 'm' and 'n' are the lower and upper limits given by the user. Then, in the inner 'for loop', we take and check if each number is prime or not.

  • m = lower limit entered by the user.
  • n = Upper limit entered by the user.

Let us take an example to understand the concept clearly,  let's print the prime number between '3' to '10'. In the outer 'for loop', we take each number from '3' to '10'. Inside the inner 'for loop', check every number for prime or not.

Take number '3' in the inner loop and check if it has only two divisors; if so, set the flag as zero, and we print the number 3 as prime. Then go to the outer loop to take the next number and do the prime number check in the inner loop, each time we encounter a prime number, we increment the counter variable. 

ALGORITHM

STEP 1: Include the Header Library to use the built-in functions in the C program.

STEP 2: Start the program execution with the main() function.

STEP 3: Accept the value of the lower and upper limit of the series from the user using printf and scanf functions.

STEP 4: Check the number Upper limit is greater than 2 using the 'if' condition. If it's not satisfied print, it's not possible to find Prime numbers.

STEP 5: Else copy the Lower limit to a temp variable and using Mod operator with 'm' and 2 is Zero if so, increment the variable 'm' by 1.

STEP 6: Open the Outer For loop from 'm' to 'n' incremented by 2 (no need to check the Even Numbers)

STEP 7: Open the Inner Loop from 2 to half to the value of the variable 'i' and increment by 1

STEP 8: Check the 'i' mod 'j' is Zero or not. Suppose it is Zero, set Flag as one, and Break the loop.

STEP 9: If the Flag is Zero after the Inner loop, print that number as Prime and increment the Count by 1.

C Source Code

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

void main() 
{
  int M, N, i, j, flag, temp, count = 0;                            // declares the variables
  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);                   /*check the numbers are greater than 2*/
     exit(0);
   }
  printf("Prime numbers are\n");
  temp = M;
  if (M % 2 == 0) 
  {
     M++;
   }
  for (i = M; i <= N; i = i + 2)                   /* start of the outer loop and set the flag as zero */ 
  {
     flag = 0;
     for (j = 2; j <= i / 2; j++)                     /* inner loop to check the number is prime */ 
     {
        if ((i % j) == 0)
        {
           flag = 1;
           break;
        }
    }
    if (flag == 0) 
    {
       printf("%d\n", I);                               /* print the number is prime if flag is zero */
       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