GO Program to check palindrome number

How to check palindrome number

Here we are explaining how to check palindrome numbers in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

 

How to check palindrome numbers in the GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to check the palindrome numbers in the Go language. Here variables Num holding the number for the palindrome check, and other variables are rem, rev, temp. First, reverse the number as rem = temp % 10 , rev = rev*10 + rem in a for-loop. Finally, check whether the reverse number equals a given number. If true, it is a palindrome, otherwise not a palindrome number. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Declare the variable Num, rem, rev, temp

STEP 4: Read the number Num using fmt.Scanfln()

STEP 5:Initialise integer rev=0

STEP 6: Reverse the given number using for loop as (for temp := palNum; temp > 0; temp = temp / 10)

STEP 7: Finding rem = temp % 10 and rev = rev*10 + rem in the loop

STEP8: Use If else statement (if Num == rev) to check whether the reverse number equals a given number

STEP9: If true, it is a palindrome, otherwise not a palindrome number.

 

GO Source Code

                                          package main
import "fmt"

func main() {
    var Num, rem int
    fmt.Print("Enter the number to check Palindrome = ")
    fmt.Scanln(&Num;)
    rev := 0
    for temp := Num; temp > 0; temp = temp / 10 {
        rem = temp % 10
        rev = rev*10 + rem
    }

    fmt.Println("The Reverse of the number = ", rev)
    if Num == rev {
        fmt.Println(Num, " is a Palindrome number")
    } else {
        fmt.Println(Num, " is Not a Palindrome number")
    }
}
                                      

OUTPUT

Enter the number to check Palindrome = 191
The Reverse of the number =  191
191  is a Palindrome number