C++ Program to Reverse a Number

C++ program to reverse a number is provided on this page.

The aim of this program is reversing the digits of a number in c++ programming language, so this program can also be called c++ program to reverse the digits of a number.

You can simply 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 original number.

C++ program to reverse a number

Here is the source code of 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

Comments

  1. You've created an excellent post. Your essay gave me some unique and useful information. Thank you for bringing this article to our attention. C++ Programming Homework Help Online

    ReplyDelete

Post a Comment