C Program to print Prime Number in a given range

In this tutorial, we will learn to write the C Program to print Prime Number in a given range.

Let’s see what is our problem statement which we will solve in this tutorial.

We will take the range in the form of an integer. After taking the input we will then find all the prime numbers existing in the given range.

For Example:

The input we have taken 3 and 15

So, the output will be 5, 7, 11 and13. Because these are the only numbers that are prime between the range 3 and 15.

How our program will behave

  • Our Program will take two integer numbers as an input to find the find the prime numbers.
  • Now we will use while loop start from first integer till smaller than the second input. This is because we only want in between two.
  • We will pick every time one number and will check it is divisible by any number other than 1.
  • If it is divisible then it is not a prime number. Otherwise our program will print that number as Prime number.

C Program to print Prime Number in a Given range

#include<stdio.h>
void main() {
  int i = 0, fNum, sNum, temp;
  printf("C Program to Print Prime number in a given range\n");
  printf("Please give the first number: ");
  scanf("%d",&fNum);
  printf("Please give the second number: ");
  scanf("%d",&sNum);
  printf("Prime numbers between %d and %d are\n",fNum,sNum );
  while (fNum<=sNum) {
    temp = 0;
    for (i = 2;i<=(fNum/2);i++) {
      if (fNum%i == 0) {
        temp=1;
        break;
      }
    }
    if (temp == 0)
      printf("%d \n", fNum);
    fNum++;
  }
}

Output

Please give the first number: 12
Please give the second number: 34
Prime numbers between 12 and 34 are
13 
17 
19 
23 
29 
31 

Explanations

  1. Header Inclusion and Main Function:
    • #include <stdio.h> includes the Standard Input Output library which is necessary for using printf and scanf.
    • void main() marks the beginning of the main function where the program starts executing.
  2. Variable Declaration:
    • int i, fNum, sNum, temp; declares four integer variables.
      • i is used as a loop counter.
      • fNum and sNum are used to store the first (starting) and second (ending) numbers of the range within which the program will look for prime numbers.
      • temp is a flag variable used to indicate whether a number is prime (temp = 0) or not (temp = 1).
  3. User Input for Range:
    • The program prompts the user to enter the starting (fNum) and ending (sNum) values of the range. These values are read and stored using scanf.
  4. Processing and Output:
    • The program prints a header line indicating it will list prime numbers between fNum and sNum.
    • It then enters a while loop that will iterate from fNum to sNum.
    • Inside the while loop, temp is initially set to 0 for each number (fNum).
    • A for loop runs from 2 to fNum/2. If fNum is divisible by any of these numbers (i.e., fNum % i == 0), temp is set to 1 and the loop breaks. This is because finding any divisor other than 1 and the number itself means the number is not prime.
    • After the for loop, if temp is still 0 (meaning no divisors were found), fNum is determined to be a prime number and it is printed.
    • The while loop then increments fNum to check the next number in the range.
  5. End of Program:
    • The program will continue to find and print prime numbers until fNum exceeds sNum. Once the loop is finished, the program automatically ends as it reaches the end of the main function.