top of page

C Program to find Prime Numbers Between given Interval

Writer's picture: Prateek ChauhanPrateek Chauhan

Given two numbers a and b as interval range,we have to find the prime numbers in between this interval.

Implementation:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a, b, i, j, flag;
    printf("Enter lower bound of the interval: ");
    scanf("%d", &a);
    
    printf("\n Enter upper bound of the interval: ");
    scanf("%d", &b);
    
    printf("\n Prime numbers between %d and %d are: ", a, b);
    
    for(i = a; i <= b; i++)
    {
        if( i == 1 || i == 0)
            continue;
        flag = 1;
        for (j = 2; j <= i / 2; j++) {
            if (i % j == 0) {
                flag = 0;
                break;
                }
        }
        if (flag == 1)
            printf("%d ", i);
    }
    return 0;
}
Output:
Enter lower bound of the interval: 1
Enter upper bound of the interval: 10
Prime numbers between 1 and 10 are: 2 3 5 7
 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Follow Programmers Door for more.

4 views0 comments

Recent Posts

See All

Comentarios


bottom of page