You are here

C++ program to reverse a number

C++ program to reverse a number: Reverse or invert the input number. This can be useful to check if an integer is a palindrome or not.

C++ program to reverse a number

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Operations
  6. {
  7.   long c;
  8.  
  9. public:
  10.   void inputNumber()
  11.   {
  12.     cout << "Input a number\n";
  13.     cin >> c;
  14.   }
  15.  
  16.   long reverseNumber()
  17.   {
  18.     long invert = 0;
  19.  
  20.     while (c != 0)
  21.     {
  22.       invert = invert * 10;
  23.       invert = invert + c%10;
  24.       c = c/10;
  25.     }
  26.  
  27.     return invert;
  28.   }
  29.  
  30. };
  31.  
  32. int main()
  33. {
  34.   long result;
  35.  
  36.   Operations t;
  37.   t.inputNumber();
  38.   result = t.reverseNumber();
  39.  
  40.   cout << "Number obtained on reversal = " << result;
  41.  
  42.   return 0;
  43. }