Display Prime Numbers Between Two Intervals in C

Exercise:

Write a C Program to display prime numbers between two intervals.

1. Display when smaller Number is Entered first

Click Here to View the Solution!
#include <stdio.h>
int main() {
    int low, high, i, flag;
    printf("Enter two numbers(intervals): ");
    scanf("%d %d", &low, &high);
    printf("Prime numbers between %d and %d are: ", low, high);
    while (low < high) {
        flag = 0;
        if (low <= 1) {
            ++low;
            continue;
        }
        // if low is a non-prime number then flag=1
       for (i = 2; i <= low / 2; ++i) {
           if (low % i == 0) {
               flag = 1;
               break;
           }
        }
       if (flag == 0) {
           printf("%d ", low);
       }

    // to check prime for next number increase low by 1
    ++low;
    }
    return 0;
}
Click Here to View the Output!
Enter two numbers(intervals): 3 9
Prime numbers between 3 and 9 are: 3 5 7
Click Here to View the Explanation!
  • This program is used to print all the prime number between two intervals.
  • The program requests the user to enter two numbers for the intervals and stores them into the variables low and high respectively using the scanf() function.
  • A while loop is initialized with the condition low < high which states that the loop will continue until low is not equal to high i.e. high-low-1 times.
  • Initially, the flag is set to 0. A condition is set in an if statement which makes sure that if the user enters a number less than 2 for low, the number will be ignored an increment will be done.
  • A for loop is used that will iterate between i = 2 and i <= low/2.
  • Inside the loop, the condition for a non-prime number is checked in the first if statement. It will check whether low is exactly divisible by i, if true, flag will become 1 and the loop will end.
  • Else, if the flag will remain 0, then low will be a prime number.
  • To check whether the next numbers between the interval are a prime number, low will be incremented by 1 until low will become equal to high.

2. Display when Larger Number is Entered first

Click Here to View the Solution!
#include <stdio.h>
int main() {
   int low, high, i, flag, temp;
   printf("Enter two numbers(intervals): ");
   scanf("%d %d", &low, &high);

   // swap if low is greater than high
   if (low > high) {
       temp = low;
       low = high;
       high = temp;
   }
   printf("Prime numbers between %d and %d are: ", low, high);
   while (low < high) {
       flag = 0;
       // ignore numbers less than 2
      if (low <= 1) {
          ++low;
          continue;
      }
      for (i = 2; i <= low / 2; ++i) {
          if (low % i == 0) {
              flag = 1;
              break;
          }
      }
      if (flag == 0) {
          printf("%d ", low);
      }
    ++low;
    }
    return 0;
}
Click Here to View the Output!
Enter two numbers(intervals): 13 6
Prime numbers between 13 and 6 are: 7 11
Click Here to View the Explanation!
  • This program is used to display all the prime numbers in an interval in the case when the larger number is entered first.
  • The program requests the user to enter two numbers for the intervals and stores them into the variables low and high respectively using the scanf() function.
  • A condition is set in the if statement that if low > high, the values of both the intervals will be swapped.
  • A while loop will be set which will continue the iterations until low will be less than high.
  • In every iteration, the value of low will be checked whether it is a prime number or not.
  • The value of low will be incremented by 1 until the value of low will become equal to high.
  • The return 0 statement is used to exit the program execution.
%d bloggers like this: