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