Start learning free

LESSON 212 OF 281 · WHILE LOOPS - CHALLENGES

Reverse a Number in C++: Building It Digit by Digit

Practice Read the full explanation

Given a non-negative integer n, return the number you get when you write its digits in reverse order.

Zeros at the end of n move to the front of the reversed number, where they don't count: 120 reversed is 21. If n is 0, the answer is 0.

 

Example 1:

Input: n = 123
Output: 321
Explanation: The digits 1, 2, 3 read backwards are 3, 2, 1.

Example 2:

Input: n = 120
Output: 21
Explanation: Backwards the digits are 0, 2, 1, and 021 is the number 21.

Example 3:

Input: n = 7
Output: 7
Explanation: A number with one digit stays the same.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 5
    Ctrl + Enter to check

    C++ · WHILE LOOPS - CHALLENGES

    Reverse Number in C++, explained

    Problem overview

    The function reverseNumber receives a non-negative integer n, up to 1,000,000,000, and returns the number you get by writing its digits in reverse order. The number 123 gives 321. Zeros at the end of n move to the front of the result, where they don't count, so 120 gives 21. A one-digit number stays the same, and 0 gives 0.

    Approach

    The function takes the digits off n from the right and puts each one on the right end of a new number. The last digit of n is the first digit of the reversed number, so the order comes out right without any extra work.

    Two integer operations do the job:

    • n % 10 is the last digit of n, and n / 10 drops it.
    • result * 10 + digit appends a digit to result: multiplying by 10 shifts the digits left and leaves a 0 at the end, and adding the digit fills it. For example 32 * 10 + 1 is 321.

    Here is the loop on n = 123, showing the state after each pass:

    n=12 result=3
    n=1 result=32
    n=0 result=321
    

    And on n = 120:

    n=12 result=0
    n=1 result=2
    n=0 result=21
    

    The first digit moved is the 0, and 0 * 10 + 0 is still 0, which is why trailing zeros vanish.

    Step by step

    1. Create result as an int starting at 0. It will hold the reversed number. If n is 0, the loop never runs and 0 is the right answer.
    2. Start a while loop that runs while n > 0.
    3. Move the last digit. Set result to result * 10 + n % 10.
    4. Drop that digit from n. Set n to n / 10. The digit is already inside result.
    5. Return result after the loop.

    The solution in C++

    class Solution {
    public:
        int reverseNumber(int n) {
            int result = 0;
            while (n > 0) {
                result = result * 10 + n % 10;
                n = n / 10;
            }
            return result;
        }
    };
    

    Some details about this code:

    • Operator precedence. *, / and % bind tighter than +, so the expression means (result * 10) + (n % 10). No parentheses are needed.
    • Overflow. Signed integer overflow is undefined behaviour in C++. Here it can't happen: the input is at most 1,000,000,000, and reversing a number of nine digits or fewer gives a number of nine digits or fewer, below 1,000,000,000. The only ten-digit input, 1000000000, reverses to 1. The reversed value of a ten-digit number could exceed the int range in general, so a version for larger inputs would need long long.
    • Integer division. n / 10 between two int values discards the remainder, so 123 becomes 12.

    Edge cases and common mistakes

    • Trailing zeros. 120 gives 21, not 021: a number doesn't keep leading zeros.
    • Zero. The loop is skipped and result stays 0.
    • Palindromes. 900000009 reverses to itself, and 987654321 gives 123456789.
    • Largest input. 1000000000 gives 1, because the nine zeros come off first and add nothing.
    • Swapping the two lines in the loop. If n is divided before n % 10 is used, the wrong digit is added.

    Testing

    • n = 123 gives 321.
    • n = 120 gives 21.
    • n = 7 gives 7.
    • n = 0 gives 0.
    • n = 987654321 gives 123456789.
    • n = 1000000000 gives 1.
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free