Saturday, 5 May 2012

Prime Numbers between two numbers in C


#include<stdio.h>
#include<conio.h>
main()
{
int m,n,temp,i,rem;
printf("Enter the values of M and N\n");
scanf("%d%d",&m,&n);
if(m>n)
{
temp=m;
m=n;
n=temp;
}
rem=(m%2);
if(rem==0)
{
m++;
}
printf("Prime numbers between %1d and %3d are - \n",m,n);
for(i=m;i<=n;i+=2)
{
if(isprime(i))
{
printf("%d\n",i);
}
}
    getch();
return 0;
}


int isprime(int k)
{
int limit,td,remain;
remain=k%2;
if(remain)
{
td=3;
limit=sqrt(k);
while(remain&&(td<=limit))
{
remain=k%td;
td+=2;
}
}
return(remain);
}

No comments:

Post a Comment