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

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is a prime number?

In this 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 one is called a prime number. This property is called primality. The largest prime number has '17,425,170' digits. For example, 7 is a prime number because 7 has only two divisors such that '1' and '7' itself.

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 complicated logic in this c program. First, check if the given numbers are greater than two; else, 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. Inside the inner 'for loop', we check if each number is prime or not.

  • M = lower limit entered by the user.
  • N = Upper limit entered by the user.

For example, let's print the prime number from '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. Finally, we increment the count if we got a prime number. 

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