A Program to Reverse an Integer

In this example, we are giving a program to reverse an integer given by user.


Program to reverse an integer:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, reversedNumber = 0, remainder;
  6. cout << "Enter an integer: ";
  7. cin >> n;
  8. while(n != 0)
  9. {
  10. remainder = n%10;
  11. reversedNumber = reversedNumber*10 + remainder;
  12. n /= 10;
  13. }
  14. cout << "Reversed Number = " << reversedNumber;
  15. return 0;
  16. }

Output:

Enter an integer: 1234
Reversed Number = 4321