C Program to Display Prime Numbers Between Two Intervals - IProgramX

Q. Write a program to display all prime numbers between ____ and ____.


Program

#include <stdio.h>
int main()
{
    int low, high, i, flag,x;
    printf("Enter two numbers : ");
    scanf("%d %d", &low, &high);
    if(low>high)
    {
     x=high;
     high=low;
     low=x;
}
    printf("Prime numbers between %d and %d are: ", low, high);
    while (low < high)
    {
        flag = 0;

        for(i = 2; i <= low/2; ++i)
        {
            if(low % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            printf("%d ", low);
        ++low;
    }
}

Output:

Enter two numbers : 2 10
Prime numbers between 2 and 10 are: 2 3 5 7

Post a Comment

0 Comments