0

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";
	 }
}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by WXuan
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.