C Program to Find the Prime Numbers

Prime number is a number that is divisible by 1 and itself.

Example prime numbers are 2, 3, 5, 7, 11, 13, 17.

#include<stdio.h>
int main ()
{
    int i, prime, up, low, n;
    printf ("ENTER THE LOWER LIMIT : ");
    scanf ("%d", &low);
    printf ("ENTER THE UPPER LIMIT : ");
    scanf ("%d", &up);
    if(low>=2)
    {
        printf ("PRIME NUMBERS ARE : ");
        for (n = low + 1; n < up; n++)
        {
            prime = 1;
            for (i = 2; i < n; i++)
                if (n % i == 0)
                {
                    prime = 0;
                    break;
                }
            if (prime)
                printf ("\t %d", n);
        }
    }
    else
    {
        printf("Enter lower number must be greater than 1");
    }

}

Output for Prime Numbers:

ENTER THE LOWER LIMIT : 2
ENTER THE UPPER LIMIT : 20
PRIME NUMBERS ARE :      3       5       7       11      13      17      19

Leave a Reply

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