Start learning free

LESSON 216 OF 289 · WHILE LOOPS - CHALLENGES

Reverse a Number in C#: Building the Result 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

    ReverseNumber receives a non-negative int n and returns the number with its digits in reverse order. 123 becomes 321. Zeros at the end of n move to the front, where they don't count: 120 reads 021, which is the number 21. A one-digit number, and 0, stay the same.

    Approach

    Take digits off the right end of n and put each one on the right end of a new number. The first digit removed ends up first in the result, and the last digit removed ends up last, so the order is reversed with no extra work.

    • n % 10 is the last digit of n.
    • n / 10 removes it (integer division drops the remainder).
    • result * 10 + digit appends a digit to the result: multiplying by 10 shifts every digit one place left and leaves a 0 at the end, and adding the digit fills that place. 32 * 10 + 1 is 321.

    Step by step

    1. Create result as an int set to 0. If n is 0 the loop never runs and 0 is right.
    2. Write while (n > 0). Each pass moves one digit.
    3. Set result to result * 10 + n % 10. The last digit of n becomes the last digit of result.
    4. Divide n by 10 and store it back in n.
    5. Return result after the loop.

    For n = 120:

    Passresult after the passn after the pass
    1012
    221
    3210

    The first digit moved is the 0, and 0 * 10 + 0 is still 0. That is why the trailing zero of 120 disappears.

    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;
        }
    }

    In result * 10 + n % 10, the operators * and % bind tighter than +, and * and % have the same precedence and go left to right. So it means (result * 10) + (n % 10), which is what you want.

    Why the digits come out reversed

    Look at where each digit lands. In 123, the digit 3 is removed first. At that moment result is 0, so it becomes 3. Then 2 is removed, and 3 * 10 + 2 makes 32. The 3 has moved one place left, and the 2 sits to its right. Finally 1 gives 32 * 10 + 1, which is 321. Every new digit goes to the right of all the earlier ones, so the digit taken first from the right of n ends at the left of the result.

    The same two lines of the loop are used for both numbers: n loses its last digit and result gains one. Together they move the digits across without any text or array.

    Edge cases and common mistakes

    • Trailing zeros. 120 gives 21, not 021. A number has no leading zeros, and an int can't store them.
    • Zero. The loop doesn't run, and the method returns 0.
    • Overflow. Reversing a number can in general produce a value that doesn't fit in an int: int overflow wraps silently in C#, with no error. Here n is at most 1,000,000,000, so the reversal has at most 9 significant digits and stays below the limit. 999999999 gives 999999999 and 1000000000 gives 1.
    • Dividing first. If you drop the digit before using it, the first digit is lost.

    Testing

    The solution returns 321 for 123, 21 for 120, 7 for 7, 0 for 0 and 1 for 1000000000.

    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