Tuesday 30 April 2013

PROGRAM TO FIND ALL THE PRIME NUMBERS WITHIN GIVEN RANGE

prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A simple but slow method of verifying the primality of a given number n is known as trial division. It consists of testing whether n is a multiple of any integer between 2 and \sqrt{n}.
This is what I have done in my C program to check whether a number is prime or not. The following program finds all the prime numbers within a given range.

Source Code :-
/******************************************************************************/
//PROGRAM TO FIND ALL THE PRIME NUMBERS WITHIN GIVEN RANGE

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
long ll,ul,i,j;
clrscr();

printf("ENTER THE LOWER LIMIT : ");
scanf("%ld",&ll);

printf("ENTER THE UPPER LIMIT : ");
scanf("%ld",&ul);

for(i=ll;i<=ul;i++)
{
if(i==1)
i++;

for(j=2;j<=sqrt(i);j++)
{
if(i%j==0)
{
goto lavish;
}
}
printf("%ld\t",i);
lavish : ;
}

getch();
return 1;
}

/****************************************************************************/
Output:-




No comments:

Post a Comment