Reverse a Number in C++
Reversing a number — turning 1234 into 4321 — is one of the most common beginner exercises, and for good reason. It teaches you the two-step pattern of pulling digits off a number one at a time, which shows up everywhere from digit sums to palindrome checks.
The Two Operations You Need
To reverse a number, you process it digit by digit using two operators:
number % 10gives you the last digit (the remainder after dividing by 10).number / 10removes the last digit (integer division drops the fraction).
For example, with 1234: 1234 % 10 is 4, and 1234 / 10 is 123. Do that repeatedly and you visit every digit from right to left.
#include <iostream>
int main() {
int number = 1234;
std::cout << "Last digit: " << number % 10 << "\n"; // 4
std::cout << "Rest: " << number / 10 << "\n"; // 123
return 0;
}
Building the Reversed Number
The reversal itself uses a while loop. Each pass, we take the last digit and append it to a reversed value. To “append,” we shift reversed one place to the left by multiplying by 10, then add the new digit:
#include <iostream>
int main() {
int number = 1234;
int reversed = 0;
while (number != 0) {
int digit = number % 10; // grab last digit
reversed = reversed * 10 + digit; // shift left, add digit
number = number / 10; // drop last digit
}
std::cout << "Reversed: " << reversed << "\n"; // 4321
return 0;
}
Trace it: reversed goes 0 -> 4 -> 43 -> 432 -> 4321, while number shrinks 1234 -> 123 -> 12 -> 1 -> 0. When number hits 0, the loop ends and reversed holds the answer.
Handling Negative Numbers
If the user might enter a negative number like -123, the cleanest approach is to remember the sign, reverse the absolute value, then put the sign back:
#include <iostream>
#include <cstdlib> // for std::abs
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
int sign = (number < 0) ? -1 : 1;
number = std::abs(number);
int reversed = 0;
while (number != 0) {
reversed = reversed * 10 + number % 10;
number /= 10;
}
std::cout << "Reversed: " << sign * reversed << "\n";
return 0;
}
Reversing the absolute value keeps the loop simple, and reapplying sign at the end gives -123 -> -321.
A Note on Overflow
Reversing a large number can produce a value too big for an int. For example, reversing 1000000009 overflows a 32-bit int. For beginner exercises with small inputs this rarely bites, but if you want to be safe, use a wider type like long long for the reversed variable:
long long reversed = 0;
This is a good habit: whenever a calculation can grow past the input’s size, give the result room to breathe.
Where This Pattern Leads
The “peel a digit, shrink the number” loop is a building block. Swap the reversed = reversed * 10 + digit line for sum += digit and you get the sum of digits. Compare the reversed number to the original and you get a palindrome check. Master this one loop and a dozen classic problems open up.
Related Articles
- Reverse a String in C++ — the string version of the same idea
- C++ Modulo Operator (%) — how digit extraction works
- Integer Division in C++ — why number / 10 drops a digit
- Palindrome Program in C++ — reverse and compare
- C++ Loops Tutorial — while loops in depth
Take Your C++ Further
If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.