C++ Program to Reverse a Number

Write a C++ program to reverse a number. Write a c++ program to reverse the digits of a number. You can simple reverse a number by extracting a last digit by using modulo division on given number. For example, if number is 1234 then 1234 % 10 will give you 4. Initialize reverse_number = 0. Now use the formula reverse_number = reverse_number + remainder * 10. Do this process until there are no digits left in the given number.

C++ program to reverse a number

/* Reverse a number in C++ */

#include<iostream>
using namespace std;
int main()
{
    int number, remainder = 0, reverse_number;

    cout << "Enter any integer: ";
    cin >> number;

    while(number != 0)
    {
        remainder = number % 10;
        reverse_number = reverse_number * 10 + remainder;
        number /= 10;
    }
    cout << "Reversed Number = " << reverse_number;
    return 0;
}

Output:

Enter any integer = 1234
Reversed Number = 4321