Answer to Question #180189 in C++ for vivek

Question #180189

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-10T22:10:30-0400
#include <iostream>
class Palindrom
{
public:
	int count_palindrome(int upp, int low) 
	{
		int num;
		int rev_num;
		int count = 0;
		for (int i = low; i <= upp;i++)
		{
			num = i;
			rev_num = 0;
			while (num > 0)
			{
				rev_num = rev_num * 10 + num % 10;
				num = num / 10;
			}
			if (rev_num == i) 
			{
				count++;
			}
		}
		return count;
	}
	Palindrom(int upper, int lower)
	{
		lower_limit = lower;
		upper_limit = upper;
		if (upper <= lower)
		{
			std::cout << "Incorrect values entered" << std::endl;
		}
		else
		{
			std::cout << "Count of palindrome numbers is: " << count_palindrome(upper, lower) << std:: endl;
		}
	}
private:
	int upper_limit{1};
	int lower_limit{ 0 };
};
int main()
{
	Palindrom example(50, 10);
	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