0
0
GoProgramBeginner · 2 min read

Go Program to Reverse a Number with Output and Explanation

In Go, you can reverse a number by repeatedly extracting its last digit using num % 10, adding it to a reversed number multiplied by 10, and then removing the last digit from the original number using num /= 10 until the original number becomes zero.
📋

Examples

Input12345
Output54321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, think of peeling off the last digit one by one using the remainder operator %. Then build the reversed number by shifting the current reversed number left by one digit (multiply by 10) and adding the peeled digit. Repeat until the original number is fully processed.
📐

Algorithm

1
Get the input number.
2
Initialize reversed number to 0.
3
While the input number is greater than 0:
4
Extract the last digit using modulus 10.
5
Multiply reversed number by 10 and add the extracted digit.
6
Remove the last digit from input number by dividing by 10.
7
Return the reversed number.
💻

Code

go
package main

import "fmt"

func reverseNumber(num int) int {
    reversed := 0
    for num > 0 {
        digit := num % 10
        reversed = reversed*10 + digit
        num /= 10
    }
    return reversed
}

func main() {
    number := 12345
    fmt.Println(reverseNumber(number))
}
Output
54321
🔍

Dry Run

Let's trace reversing 12345 through the code

1

Initial values

num = 12345, reversed = 0

2

First iteration

digit = 12345 % 10 = 5; reversed = 0*10 + 5 = 5; num = 12345 / 10 = 1234

3

Second iteration

digit = 1234 % 10 = 4; reversed = 5*10 + 4 = 54; num = 1234 / 10 = 123

4

Third iteration

digit = 123 % 10 = 3; reversed = 54*10 + 3 = 543; num = 123 / 10 = 12

5

Fourth iteration

digit = 12 % 10 = 2; reversed = 543*10 + 2 = 5432; num = 12 / 10 = 1

6

Fifth iteration

digit = 1 % 10 = 1; reversed = 5432*10 + 1 = 54321; num = 1 / 10 = 0

7

Loop ends

num is 0, return reversed = 54321

numdigitreversed
1234555
1234454
1233543
1225432
1154321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, like peeling one digit off.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the new digit to append it.

Step 3: Remove last digit

Divide the original number by 10 using num /= 10 to remove the last digit and continue.

🔄

Alternative Approaches

String conversion
go
package main

import (
    "fmt"
    "strconv"
)

func reverseNumberString(num int) int {
    s := strconv.Itoa(num)
    reversedStr := ""
    for i := len(s) - 1; i >= 0; i-- {
        reversedStr += string(s[i])
    }
    reversedNum, _ := strconv.Atoi(reversedStr)
    return reversedNum
}

func main() {
    fmt.Println(reverseNumberString(12345))
}
This method converts the number to a string and reverses it, which is easier to understand but less efficient due to string operations.
Recursive approach
go
package main

import "fmt"

func reverseNumberRec(num, reversed int) int {
    if num == 0 {
        return reversed
    }
    return reverseNumberRec(num/10, reversed*10+num%10)
}

func main() {
    fmt.Println(reverseNumberRec(12345, 0))
}
Uses recursion to reverse the number, which is elegant but can cause stack overhead for very large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is constant, O(1).

Which Approach is Fastest?

The arithmetic approach is fastest and uses least memory compared to string conversion or recursion.

ApproachTimeSpaceBest For
Arithmetic loopO(d)O(1)Efficient and simple for all numbers
String conversionO(d)O(d)Easy to understand but less efficient
RecursiveO(d)O(d)Elegant but uses more stack memory
💡
Use modulus and division by 10 repeatedly to peel digits and build the reversed number.
⚠️
Forgetting to multiply the reversed number by 10 before adding the new digit causes incorrect results.