×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

C++ program to print palindrome numbers in a range using class

Given a range, we have to print palindrome numbers in that range using the class and object approach.
Submitted by Shubh Pachori, on August 22, 2022

Example:

Input: 
Enter Range: 11 50

Output: 
Palindrome Numbers in the range 11 to 50 are 
11 22 33 44

C++ code to print palindrome numbers in a range using the class and object approach

#include <iostream>
using namespace std;

// create a class
class IsPalindrome {
  // declare private data members
  private:
    int start, end;

  // public functions
  public:

    // getRange() function to insert range
    void getRange() {
      cout << "Enter Range:";
      cin >> start >> end;
    }

  // isPalindrome() function to print palindrome 
  // numbers in between range
  void isPalindrome() {

    // initialize int type variable for indexing
    int index;

    cout << "Palindrome Numbers in the range " << start << " to " << end << " are " << endl;

    // for loop to search palindrome number in the range
    for (index = start; index <= end; index++) {

      // initilising int type variables to 
      // perform operations
      int remain, result = 0;

      // copying value of index in number variable
      int number = index;

      // while loop for manipulating the number
      while (number) {

        // remain contains the last element of the number
        remain = number % 10;

        // remain added to it
        result = (result * 10) + remain;

        // number is divided by 10 at each iteration 
        // to get new number
        number = number / 10;
      }

      // if the result is equal to the index 
      // then it will print it
      if (result == index) {
        cout << index << " ";
      }
    }
  }
};

int main() {
  // create an object
  IsPalindrome P;

  // calling getRange() function to insert range
  P.getRange();

  // isPalindrome() function search 
  // palindrome numbers in the range
  P.isPalindrome();

  return 0;
}

Output

RUN 1:
Enter Range:50 100
Palindrome Numbers in the range 50 to 100 are 
55 66 77 88 99

RUN 2:
Enter Range:1 100
Palindrome Numbers in the range 1 to 100 are 
1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99

Explanation

In the above code, we have created a class IsPalindrome, two int type data members start and end to store the start and end of the range, and public member functions getRange() and isPalindrome() to store and print palindrome numbers in between that given range.

In the main() function, we are creating an object P of class IsPalindrome, reading a range by the user using getRange() function, and finally calling the isPalindrome() member function to print the palindrome numbers in the given range. The isPalindrome() function contains the logic to print palindrome numbers in the given range and printing the result.

C++ Class and Object Programs (Set 2) »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.