0
0
GoProgramBeginner · 2 min read

Go Program to Check Palindrome Number

In Go, you can check if a number is a palindrome by reversing the number using a loop and comparing it to the original number, for example: if original == reversed { fmt.Println("Palindrome") }.
📋

Examples

Input121
Output121 is a palindrome number
Input123
Output123 is not a palindrome number
Input0
Output0 is a palindrome number
🧠

How to Think About It

To check if a number is a palindrome, think of reading it from left to right and right to left. If both are the same, it is a palindrome. We reverse the number by taking its last digit repeatedly and building a new number, then compare it with the original.
📐

Algorithm

1
Get the input number and store it as original.
2
Initialize reversed number as 0.
3
While the number is greater than 0, extract the last digit and add it to reversed after shifting digits.
4
Remove the last digit from the number.
5
Compare reversed with original; if equal, it is a palindrome, else not.
💻

Code

go
package main

import "fmt"

func main() {
    var num, original, reversed, digit int
    fmt.Print("Enter a number: ")
    fmt.Scan(&num)
    original = num
    reversed = 0

    for num > 0 {
        digit = num % 10
        reversed = reversed*10 + digit
        num /= 10
    }

    if original == reversed {
        fmt.Printf("%d is a palindrome number\n", original)
    } else {
        fmt.Printf("%d is not a palindrome number\n", original)
    }
}
Output
Enter a number: 121 121 is a palindrome number
🔍

Dry Run

Let's trace the number 121 through the code to see how it checks palindrome.

1

Initialize variables

num=121, original=121, reversed=0

2

First loop iteration

digit = 121 % 10 = 1; reversed = 0*10 + 1 = 1; num = 121 / 10 = 12

3

Second loop iteration

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

4

Third loop iteration

digit = 1 % 10 = 1; reversed = 12*10 + 1 = 121; num = 1 / 10 = 0

5

Compare original and reversed

original=121, reversed=121, they are equal, so palindrome

numdigitreversed
12111
12212
11121
💡

Why This Works

Step 1: Extract digits from the number

Using num % 10 gets the last digit of the number to build the reversed number.

Step 2: Build the reversed number

Multiply reversed by 10 and add the digit to shift digits left and append the new digit.

Step 3: Compare original and reversed

If the reversed number equals the original, the number reads the same forwards and backwards, so it is a palindrome.

🔄

Alternative Approaches

Convert number to string and check
go
package main

import (
    "fmt"
    "strconv"
)

func main() {
    var num int
    fmt.Print("Enter a number: ")
    fmt.Scan(&num)
    s := strconv.Itoa(num)
    rev := ""
    for i := len(s) - 1; i >= 0; i-- {
        rev += string(s[i])
    }
    if s == rev {
        fmt.Printf("%d is a palindrome number\n", num)
    } else {
        fmt.Printf("%d is not a palindrome number\n", num)
    }
}
This method is easier to understand but uses extra memory for string conversion.
Recursive approach to check palindrome
go
package main

import "fmt"

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

func main() {
    var num int
    fmt.Print("Enter a number: ")
    fmt.Scan(&num)
    if num == reverse(num, 0) {
        fmt.Printf("%d is a palindrome number\n", num)
    } else {
        fmt.Printf("%d is not a palindrome number\n", num)
    }
}
This uses recursion to reverse the number but may be harder for beginners.

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 integer reversal method is fastest and uses least memory compared to string conversion or recursion.

ApproachTimeSpaceBest For
Integer reversalO(d)O(1)Performance and memory efficiency
String conversionO(d)O(d)Simplicity and readability
Recursive reversalO(d)O(d)Learning recursion concepts
💡
Use integer operations to reverse the number for better performance and less memory use.
⚠️
Forgetting to reset or preserve the original number before modifying it causes wrong comparisons.