Menu Close

Go – Program to Check Whether a Number is Palindrome or Not in go golang.

In this article, We are going to learn about code and algorithm to check whether a number is palindrome or not in go golang. A palindrome number is a number which remains the same when it’s digits are reversed

There are many ways to to check whether a number is palindrome or Not in go golang

1:)

Example:

1331 is a palindrome number because reverse of 1331 is 1331.

123 is not a palindrome number because reverse of 123 is 321 . After reversing the number, they are not equal.

Algorithm:

  • Get the number as input from user.
  • Reverse the number and store into a temp variable.
  • Compare temp variable to number. If both are equal then it is a palindrome number else not.

Code:

package main

import "fmt"

func main(){

    var number int

    var reverse_no int = 0

    fmt.Print("Enter the number: ")

    fmt.Scan(&number)

    tmp_number := number

    for {
        remainder := tmp_number % 10

        reverse_no = reverse_no * 10 + remainder

        tmp_number = tmp_number / 10

       if (tmp_number == 0) {

           break

       }

    }

    if (reverse_no == number) {

        fmt.Printf("%d is a palindrome number", number)

    } else {

        fmt.Printf("%d is not a palindrome number", number)

    }
}

Output:

 Enter the number: 121
121 is a palindrome number.

Enter the number: 132
132 is not a palindrome number.

Reference:

https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println

To learn more about golang, Please follow given below link:

https://techieindoor.com/go-lang-tutorial/
Posted in golang, golang program

Leave a Reply

Your email address will not be published. Required fields are marked *