Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to check whether a given number is a palindrome or not
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
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
Advertisements