Go Palindrome Number Program

A number can be palindrome if it remains the same when we reverse it. For instance, 242 is a palindrome number. This Go program uses for loop to check whether the given number is palindrome or not. First, we used for loop (for temp := palNum; temp > 0; temp = temp / 10) to reverse the given number. Next, we used the If else statement (if palNum == reverse) to check whether the reverse number equals a given number. If True, it is a palindrome; otherwise, not a palindrome number.

package main

import "fmt"

func main() {

    var palNum, remainder int

    fmt.Print("Enter the Number to check Palindrome = ")
    fmt.Scanln(&palNum)

    reverse := 0

    for temp := palNum; temp > 0; temp = temp / 10 {
        remainder = temp % 10
        reverse = reverse*10 + remainder
    }

    fmt.Println("The Reverse of the Given Number = ", reverse)
    if palNum == reverse {
        fmt.Println(palNum, " is a Palindrome Number")
    } else {
        fmt.Println(palNum, " is Not a Palindrome Number")
    }
}
SureshMac:GoExamples suresh$ go run palindrome1.go
Enter the Number to check Palindrome = 191
The Reverse of the Given Number =  191
191  is a Palindrome Number
SureshMac:GoExamples suresh$ go run palindrome1.go
Enter the Number to check Palindrome = 125
The Reverse of the Given Number =  521
125  is Not a Palindrome Number

Go Program to Check Palindrome Number using Functions

package main

import "fmt"

var reverse int = 0

func revNumber(palNum int) int {
    var remainder int

    for ; palNum > 0; palNum = palNum / 10 {
        remainder = palNum % 10
        reverse = reverse*10 + remainder
    }
    return reverse
}
func main() {

    var palNum int

    fmt.Print("Enter the Number to check Palindrome = ")
    fmt.Scanln(&palNum)

    reverse = revNumber(palNum)
    fmt.Println("The Reverse of the Given Number = ", reverse)

    if palNum == reverse {
        fmt.Println(palNum, " is a Palindrome Number")
    } else {
        fmt.Println(palNum, " is Not a Palindrome Number")
    }
}
SureshMac:GoExamples suresh$ go run palindrome2.go
Enter the Number to check Palindrome = 1331
The Reverse of the Given Number =  1331
1331  is a Palindrome Number
SureshMac:GoExamples suresh$ go run palindrome2.go
Enter the Number to check Palindrome = 2112
The Reverse of the Given Number =  2112
2112  is a Palindrome Number

In this Golang example, we declared a recursive function to find the palindrome number.

package main

import "fmt"

var reverse int = 0

func revNumber(palNum int) int {
    var remainder int

    if palNum > 0 {
        remainder = palNum % 10
        reverse = reverse*10 + remainder
        revNumber(palNum / 10)
        return reverse
    } else {
        return 0
    }
}
func main() {

    var palNum int

    fmt.Print("Enter the Number to check Palindrome = ")
    fmt.Scanln(&palNum)

    reverse = revNumber(palNum)
    fmt.Println("The Reverse of the Given Number = ", reverse)

    if palNum == reverse {
        fmt.Println(palNum, " is a Palindrome Number")
    } else {
        fmt.Println(palNum, " is Not a Palindrome Number")
    }
}
Enter the Number to check Palindrome = 121
The Reverse of the Given Number =  121
121  is a Palindrome Number

This Go example will print the Palindrome numbers between the minimum and maximum limit value.

package main

import "fmt"

func main() {

    var palNum, temp, reverse, palMin, palMax, remainder int

    fmt.Print("Enter the Minimum and Maximum limit of Palindrome = ")
    fmt.Scanln(&palMin, &palMax)

    fmt.Print("Palindrome Numbers between ", palMin, " and ", palMax, " are : ")
    for palNum = palMin; palNum <= palMax; palNum++ {
        reverse = 0
        for temp = palNum; temp > 0; temp = temp / 10 {
            remainder = temp % 10
            reverse = reverse*10 + remainder
        }
        if palNum == reverse {
            fmt.Print(palNum, "\t")
        }
    }
}
Golang Palindrome Number Program 4