Start learning free

LESSON 217 OF 285 · WHILE LOOPS - CHALLENGES

Reverse Number in Go: Building Digits with a Loop

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

    GO · WHILE LOOPS - CHALLENGES

    Reverse Number in Go, explained

    Problem overview

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

    Approach

    Take the digits off n from the right, and push each one onto the right end of a new number. The last digit of n comes off first, and it becomes the first digit of the result, so the order is reversed with no extra work.

    Two tools do it, both with integers:

    • n % 10 and n / 10 take the last digit off n, as in any digit loop.
    • result * 10 + digit appends a digit to result: multiplying by 10 shifts the digits one place left and leaves a 0 at the end for the new one. 32 * 10 + 1 is 321.

    For n = 123, the values after each pass are:

    Passn afterwardsresult
    1123
    2132
    30321

    For n = 120 the first digit moved is 0, and 0 * 10 + 0 is still 0, so it leaves no trace:

    Passn afterwardsresult
    1120
    212
    3021

    Why it works: after each pass, result holds the digits taken off so far, in the order they were taken, and n holds the digits still waiting. When n reaches 0, nothing is waiting and result holds them all, reversed.

    Step by step

    1. Create result with :=, starting at 0. No digit has been moved yet. For n = 0 the loop never runs and 0 is correct.
    2. Loop while n > 0, with a for that has only a condition, since Go has no while.
    3. Move the last digit: set result to result * 10 + n % 10.
    4. Drop that digit from n with n = n / 10.
    5. Return result.

    The solution in Go

    func reverseNumber(n int) int {
        result := 0
        for n > 0 {
            result = result * 10 + n % 10
            n = n / 10
        }
        return result
    }
    

    Go details:

    • Precedence. *, / and % bind tighter than +, so result * 10 + n % 10 means (result * 10) + (n % 10), which is what the loop needs.
    • Room for the result. An int is 64 bits in Go 1.22 on 64-bit systems, so reversing a ten-digit number can't overflow. reverseNumber(999999999) returns 999999999.

    The same reversal is how a palindrome number is checked: a number is a palindrome when it equals its reverse. With this function, reverseNumber(121) == 121 is true, while reverseNumber(123) == 123 and reverseNumber(10) == 10 are false. Notice the last one: because trailing zeros vanish, 10 reverses to 1.

    Complexity

    One pass per digit, so the time grows with the number of digits (at most ten here). The space is constant: two integers.

    Edge cases and common mistakes

    • Trailing zeros. reverseNumber(120) is 21, not 021. An integer has no leading zeros to keep.
    • Zero. reverseNumber(0) returns 0.
    • Largest input. reverseNumber(1000000000) returns 1.
    • Swapping the two lines in the loop. If n is divided first, the digit you add is the wrong one.
    • Forgetting the multiplication. result + n % 10 would just add the digits together, giving 6 for 123 instead of 321.

    Testing

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