Display Prime Numbers Between Intervals in C


Write a C Program to display prime numbers between intervals using function.

Click Here to View the Solution!
#include <stdio.h>
int checkPrimeNumber(int num);

int main() {
    int num1, num2, i, flag;
    printf("Enter two positive integers: ");
    scanf("%d %d", &num1, &num2);
    printf("Prime numbers between %d and %d are: ", num1, num2);
    for (i = num1 + 1; i < num2; ++i) {
        flag = checkPrimeNumber(i);
        if (flag == 1)
            printf("%d ", i);
    return 0;

int checkPrimeNumber(int num) {
    int j, flag = 1;
        for (j = 2; j <= num / 2; ++j) {
            if (num % j == 0) {
                flag = 0;
    return flag;
Click Here to View the Output!
Enter two positive integers: 3 9
Prime numbers between 3 and 9 are: 5 7 
Click Here to View the Explanation!
  • This program is used to display all the prime numbers between the intervals using a user-defined function.
  • A user-defined function integer checkPrimeNumber is declared with an integer a as its parameter.
  • In main(), The program requests the user to enter two positive integers as intervals and stores them in the variables num1 and num2 using the scanf() function.
  • A for loop is initialized that will iterate between i = num1 and i < num2.
  • The checkPrimeNumber function takes i as a parameter and stores the output in the variable flag. If flag will be equal to 1, the number will be a prime number and will be printed.
  • The checkPrimeNumber will be formed that will use a for loop which will iterate between j = 2 and j = num/2 and will increment j in each iteration. A flag will be set to 1 initially.
  • An if statement will check whether num is exactly divisible by j, if true, will turn flag to 0. Else, the flag will remain 1 and the number will be a prime number.