Wednesday, 27 August 2014

Filled Under: ,

C++ program to reverse a number

Share
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


C++ program to reverse a number


#include <iostream>
 

class Operations
{
  long c;
 
public:
  void inputNumber()
  {
    cout << "Input a number\n";
    cin >> c;
  }
 
  long reverseNumber()
  {
    long invert = 0;
 
    while (c != 0)
    {
      invert = invert * 10;
      invert = invert + c%10;
      c = c/10;
    }
 
    return invert;
  }
 
};
 
int main()
{
  long result;
 
  Operations t;
  t.inputNumber();
  result = t.reverseNumber();
 
  cout << "Number obtained on reversal = " << result;
 
  return 0;
}


Output

Input a number 465
Number obtained on reversal = 564