Thursday 9 June 2016

Print prime numbers in a given range


A prime number is a number divisible by only 1 and itself.

A number would never be divisible by numbers greater than half of itself.Suppose if you need to check whether 17 is a prime number then just check whether 17 is divisible by numbers<= 17/2.Numbers greater than 17/2 i.e greater than 8 would never divide 17 completely as they are greater than half of 17.

This program prints the prime numbers from start to end.


Code

#include<stdio.h>

int main()
{
    int no,i,start,end;

    printf("Enter start and end ");
    scanf("%d %d",&start,&end);

    for(no=start;no<=end;no++)
    {
        i=2;

        while(i<=no/2)
        {
            if(no%i)
            {
                i++;
                continue;
            }
            else
                break;
        }

        if(i==(no/2)+1)
            printf("\n%d",no);
    }

    return 0;
}



O/P




No comments:

Post a Comment