Printing prime # in given range //for large nos.

rhoit 0 Tallied Votes 123 Views Share

Just playing with the no.

points used in the program:
1. Even no cannot be prime except 2
2. % division of odd by even cannot be 0

# 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";
	 }
}