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>
#include <string>
using namespace std;
class Palindrome
{
private:
int upper_limit;
int lower_limit;
public:
bool isPalindrome(int number)
{
int reversedNumber = 0;
string numberToString = to_string(number);
for(int i = numberToString.size()-1; i >=0; i--)
{
reversedNumber*=10;
reversedNumber+=(int)numberToString[i] - (int)'0';
}
return reversedNumber == number;
}
Palindrome(int upper_limit, int lower_limit)
{
if(upper_limit >= lower_limit)
{
this->upper_limit = upper_limit;
this->lower_limit = lower_limit;
cout << "upper limit: " << upper_limit << endl;
cout << "lower limit: " << lower_limit << endl;
int palindromesCounter = 0;
for(int number = lower_limit; number <= upper_limit; number++)
{
if (isPalindrome(number))
palindromesCounter++;
}
cout << "count of palindrome numbers is: " << palindromesCounter << endl;
}
else
cout << "upper limit must be greater than lower limit" << endl;
}
};
int main()
{
Palindrome palindrome(50,10);
return 0;
}
Comments
Leave a comment