Printing prime no in given range //Using efficient algorithm for large nos.
# include<iostream.h>
# include<math.h>
void main()
{
int flag=0;
unsigned long l, h, temp;
cout<<"Enter the lower limit:"; cin>>l;
cout<<"Enter the higher limit:"; cin>>h;
if(l%2==0)
{
if(l==2) cout<<"2\t";
l++; //taking only odd no
}
for(;l<=h;l+=2, flag=0)
{
temp=sqrt(l);
if(temp%2==0) temp++; //taking only odd no
for(; temp>2; temp--)
{
if(l%temp==0)
{
flag=1;
break;
}
}
if(flag==0) cout<<l<<"\t";
}
}
WXuan
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.