Answer to Question #180838 in C++ for ishani

Question #180838

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].



1
Expert's answer
2021-04-15T06:23:38-0400
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; 
} 




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS