Answer to Question #178189 in C++ for Yajur Sharma

Question #178189

Create a class: “Palindrome” 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 palindrome numbers from upper_limit

to lower_limit. [Example: upper_limit:50, lower_limit:10, count of palindrome numbers is:

4(11 , 22, 33 and 44), also make sure the value of upper_limit should be greater than

lower_limit].


1
Expert's answer
2021-04-12T08:51:43-0400


#include<iostream>


using namespace std;




class Palindrome{
private:
	// private data members: upper_limit (int), lower_limit (int)
	int lower_limit;
	int upper_limit;
public:
	// parameterized constructor, which initializes the values of both data members.
	Palindrome(int lower_limit,int upper_limit){
		this->lower_limit=lower_limit;
		this->upper_limit=upper_limit;
		if(this->upper_limit<=this->lower_limit){
			cout<<"The value of upper_limit should be greater than lower_limit.\n";
		}else{
			int numberPalindrome = 0;
			for (int i = this->lower_limit; i <= this->upper_limit;i++)
			{
				int tempNumber=i;
				int tempRevNumber=0;
				while (tempNumber > 0)
				{
					tempRevNumber =tempRevNumber* 10 + tempNumber % 10;
					tempNumber =tempNumber/ 10;
				}
				
				if (tempRevNumber == i) 
				{
					numberPalindrome++;
				}
			}
			// constructor also displays the count of palindrome numbers from upper_limit to lower_limit.
			cout<<"The count of palindrome numbers: "<<numberPalindrome<<"\n";
		}


	}
};
int main(){


	int upper_limit=0, lower_limit=0;
	//Read lower limit from the user 
	cout<<"Enter lower limit: ";
	cin>>lower_limit;
	upper_limit=lower_limit;
	while(upper_limit<=lower_limit){
		//Read upper limit from the user 
		cout<<"Enter upper limit: ";
		cin>>upper_limit;	
	}
	//create Palindrome object
	Palindrome palindrome(lower_limit,upper_limit);

	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