Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Reverse a Number: Trailing Zeros and Overflow in C++

Reverse an integer's digits in C++ and test the inputs that break it: trailing zeros that vanish, negative numbers, and reverses too big for a 32-bit int.

BeginnerAdvanced Number ProgramsExample 4 of 13
reverse-a-number-trailing-zeros-and-overflow.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int num, reversed = 0, remainder;
6
7 cout << "Enter a number: ";
8 cin >> num;
9
10 int original = num;
11
12 while (num != 0) {
13 remainder = num % 10;
14 reversed = reversed * 10 + remainder;
15 num /= 10;
16 }
17
18 cout << "Reverse of " << original << " is: " << reversed << endl;
19
20 return 0;
21}

Sample Output

Reverse of 1234 is: 4321

No zeros or overflow involved.

In this one

  • Why trailing zeros disappear when reversing an int
  • How % and / behave with negative numbers
  • How to detect overflow before it happens
  • When a string reverse is the better tool

How it works

  1. 1

    Save original = num.

  2. 2

    Take the last digit with num % 10.

  3. 3

    Append it with reversed * 10 + digit.

  4. 4

    Drop it with num /= 10 and repeat until num is 0.

  5. 5

    Print original and reversed.

Edge cases

Reverse too big for int

Enter 1999999999.

The reverse 9999999991 exceeds INT_MAX, so the final multiply overflows, which is undefined behavior. Use long long or check before multiplying.

Trailing zeros

Enter 1200.

The output is 21. Reversing 21 again gives 12, not 1200, so the zeros are lost for good.

What's going on

Same loop as the beginner page, so this one skips the basics and goes after the inputs that make digit reversal go wrong: trailing zeros, negatives, and numbers whose reverse is too big for an int.

Quick recap of the code. original = num keeps the input. While num != 0: remainder = num % 10 takes the last digit, reversed = reversed * 10 + remainder pushes it onto the right of the result, and num /= 10 drops it. 1234 becomes 4321.

Trailing zeros. 1200 reverses to 21, not 0021. During the first two passes reversed is 0 * 10 + 0 = 0, so the zeros leave no trace. An integer simply has no way to store leading zeros. If the zeros matter (an ID number, a PIN), reverse the digits as a string instead.

Negatives. In C++11 and later, % keeps the sign of the left side, so -123 % 10 is -3 and -123 / 10 is -12. The loop still ends because num heads toward 0, and the result is -321. That happens to look right, but it is a side effect of the rules, not a design. A cleaner habit is to remember the sign, reverse the absolute value, and put the sign back.

Overflow. The largest 32-bit int is 2147483647. Enter 1999999999: it fits, but its reverse, 9999999991, does not. Somewhere in the last pass reversed * 10 exceeds the limit. Signed overflow is undefined behavior in C++; in practice you usually see a strange negative number. The fix is to check before multiplying: if reversed > (INT_MAX - remainder) / 10, stop and report an error. Or store reversed in a long long, which holds any reversed 32-bit value.

Zero. while (num != 0) is false at once, so reversed stays 0 and the output is right with no special case.

A neat property: reversing twice gives back the original only when the number has no trailing zeros. 1200 → 21 → 12. That is a quick test for whether your loop dropped something.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Reverse of 5000 in this program?

Pick an answer to check it.

Question 2

What is -47 % 10 in C++11?

Pick an answer to check it.

Questions people ask

How is this page different from Reverse a Number Using a While Loop?

The code is the same. That page teaches the loop step by step; this one focuses on the inputs where the result is wrong or undefined.

How do I keep leading zeros?

Treat the number as text: string s = to_string(n); reverse(s.begin(), s.end());

Does the loop work for 0?

Yes. The loop is skipped and 0 is printed.