Display Prime Numbers Between Intervals Using Function

Write a program to Display Prime Numbers Between Intervals Using Function.

Prime Number: A number which is divisive by 1 and itself then the number is prime otherwise the number is not a prime number.

Note: 1 is not a prime


C Program

#include <stdio.h>

void primeRange(int startint end)
{

    for (int i = starti <= endi++)
    {
        if (i == 1)
        {
            continue;
        }
        else
        {
            int flag = 0;
            for (int j = 2j < ij++)
            {
                if (i % j == 0)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                printf("%d "i);
            }
        }
    }
}
int main()
{
    int startend;

    printf("Enter starting number : ");
    scanf("%d", &start);

    printf("Enter ending number : ");
    scanf("%d", &end);

    printf("Prime number in range %d to %d are:\n"startend);
    primeRange(startend);

    return 0;
}
Input:
Enter starting number : 1
Enter ending number : 20

Output:

Prime number in range 1 to 20 are:
2 3 5 7 11 13 17 19 


No comments:

Post a Comment