Write a Golang program to check whether a given number is a palindrome or not

Go ProgrammingServer Side ProgrammingProgramming

Definition: A palindrome is a number which is similar when read from the front and from the rear.

Examples

  • num = 121 => Palindrome
  • num = 13131 => Palindrome
  • num = 123 => Not a Palindrome

Approach to solve this problem

  • Step 1: Define a function that accepts a numbers(num); type is int.
  • Step 2: Start making the number from the input number.
  • Step 3: If the given number is same as the output number, then return “Palindrome”
  • Step 4: Else, return “Not A Palindrome”

Program

Live Demo

package main
import "fmt"
func checkPalindrome(num int) string{
   input_num := num
   var remainder int
   res := 0
   for num>0 {
      remainder = num % 10
      res = (res * 10) + remainder
      num = num / 10
   }
   if input_num == res {
      return "Palindrome"
   } else {
      return "Not a Palindrome"
   }
}

func main(){
   fmt.Println(checkPalindrome(121))
   fmt.Println(checkPalindrome(123))
   fmt.Println(checkPalindrome(1331))
   fmt.Println(checkPalindrome(1231))
}

Output

Palindrome
Not a Palindrome
Palindrome
Not a Palindrome
raja
Published on 04-Feb-2021 15:26:39
Advertisements