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].
#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;
}
Comments
Leave a comment