C Program to Display Prime Numbers Between Two Intervals

In this post, we will learn how to display prime numbers between two intervals using C Programming language.

C Program to Display Prime Numbers Between Two Intervals

After executing the program, the user is asked to enter the from and to range and then the program will display all the prime numbers lying in the range in a sequential manner.

So, without further ado, let’s begin this tutorial.

C Program to Display Prime Numbers Between Two Intervals

// C Program To Find Prime Numbers in a Given Range
#include <stdio.h>
int main(){
    int a, b, num, i, j;
    
    // Asking for Input
    printf("Enter First Number: ");
    scanf("%d", &a);
    
    printf("Enter Second Number: ");
    scanf("%d", &b);
    
    // Displays output
    printf("Prime Numbers between %d and %d are:", a, b);
    
    for (i = a + 1; i < b ; ++i){
        num = 0;
        for (j = 2; j <= i/2; ++j){
            if (i % j == 0){
                num = 1;
                break;
            }
        }
    if (num == 0){
        printf("%d\n", i);
    }
    }
    return 0;
}

Output

Enter First Number: 5
Enter Second Number: 25
Prime Numbers between 5 and 25 are:7
11
13
17
19
23

How Does This Program Work ?

    int a, b, num, i, j;

In this example, we have declared five int data type variables named as a, b, num, i and j.

    // Asking for Input
    printf("Enter First Number: ");
    scanf("%d", &a);
    
    printf("Enter Second Number: ");
    scanf("%d", &b);

Then, the user is asked to enter the range. This value will get stored in a and b named variables respectively.

    for (i = a + 1; i < b ; ++i){
        num = 0;
        for (j = 2; j <= i/2; ++j){
            if (i % j == 0){
                num = 1;
                break;
            }
        }
    if (num == 0){
        printf("%d\n", i);
    }
    }

Then, we have written the logic to check which of the numbers is the prime number. If the number belongs to the prime number, then it is displayed on the screen.

Some of the used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. 

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. 

scanf() – scanf() function is used to take input from the user.  

printf() – printf() function is used to display and print the string under the quotation to the screen.  

for loop – A loop is used for initializing a block of statements repeatedly until a given condition returns false.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. 

% – It is known as Modulus Operator and provides remainder after division.

// – Used for Commenting in C. 

Conclusion

I hope after going through this post, you understand how to display prime numbers between two intervals using C Programming language.

If you have any query regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *