1. Create a class: “Prime” with private data members: upper_limit (int), lower_limit (int) and parameterized constructor, which initializes the values of both data members. Apart from initialization, this constructor also displays the count of prime numbers from upper_limit to lower_limit. [Example: upper_limit:15, lower_limit:10, count of prime numbers is:2 (11 and 13), also make sure the value of upper_limit should be greater than lower_limit].
using namespace std;
class Prime
{
public:
int lower_bound,upper_bound;
void SetLimits(int l, int u)
{
lower_bound=l;
upper_bound=u;
}
char IsPrime(int Num)
{
int i, flag=1;
for (i = 2; i <= (Num/2); i++)
{
if (Num % i == 0)
{
flag = 0;
break;
}
}
return(flag);
}
};
main(void)
{
int m=1,n=0,u,c=0;
char Flag;
class Prime P;
while(m>n)
{
cout<<"\nEnter Lower Bound: "; cin>>m;
cout<<"\nEnter Upper Bound: "; cin>>n;
if(m>n) cout<<"\nLower bound should be less than or equal to upper bound.\n";
}
P.SetLimits(m,n);
cout << "Following are the prime numbers between "<<m<<" and "<<n<<" : "<<endl;
for(u=m;u<=n;u++)
{
if(P.IsPrime(u)) {cout<<u<<", "; c++;}
}
cout<<"\nNo. of Prime Nos. = "<<c;
return 0;
}
Comments
Leave a comment