# Haskell Program To Check Whether The Input Number Is A Palindrome

This tutorial discusses writing a program to check whether the input number is a Palindrome number in the Haskell programming language.

A number can be called as a palindrome when it results in the same number when the digits are reversed. For Example, the number 12321 is a palindrome, because after reversing the digits it results in the same number.

In this tutorial we see,

• Program to check whether the input number is a palindrome using the string reverse function.

• Program to check whether the input number is a palindrome using the recursive function.

## Method 1: Checking Palindrome using String Reverse Function

### Algorithm Steps

• We declared a function isPalindrome which takes an integer as an argument and returns a boolean value.

• Using the "Where" keyword to express the logic in multiple statements

• Converting the number to number to string using the function show and

• Reversing the string by using the function reverse

• Comparing both the strings

• Printing the results as per the boolean expression got from comparison

### Example

-- function declaration
isPalindrome :: Int->Bool

-- function definition
isPalindrome n = (n == k)
where
-- Converting integer to string using function show
nStr = reverse (show n)
-- Converting string to an integer using function read
k = (read nStr :: Int)

main :: IO()
main = do
-- initializing variable num
let num = 12321
-- invoking the function isPalindrome
let status = isPalindrome num
-- printing the status
if(status==True)
then print ("The number " ++ show num ++ " is a palindrome")
else print ("The number " ++ show num ++ " is not a palindrome")


### Output

"The number 12321 is a palindrome"


## Method 2: Checking Palindrome using String Recursive Function

### Algorithm

• We declared a function isPalindrome as such it takes three integer arguments and returns a boolean value.

• Create a main function and define a value to check whether it is a palindrome or not.

• Initiailize the Palendrom function in the main function towards the defined number.

• Print the result as per the boolean expression got from the Palendrom function.

### Example

Program to check whether the input number is a palindrome using the recursive function

-- function declaration
isPalindrome :: Int->Int->Int->Bool
-- function definition
isPalindrome num a revNum = if(a==0)
then if (revNum == num)
then True
else False
else k
where
d = mod a 10
newRevNum = revNum*10 + d
newA = div a 10
k = isPalindrome num newA newRevNum

main :: IO()
main = do
-- initializing variable num
let num = 1256521
-- invoking the function isPalindrome
let status = isPalindrome num num 0
-- printing the status
if(status==True)
then print ("The number " ++ show num ++ " is a palindrome")
else print ("The number " ++ show num ++ " is not a palindrome")


### Output

"The number 1256521 is a palindrome"


## Conclusion

In this tutorial, we discussed two ways to implement a program to check whether the number is a palindrome in the Haskell programming Language.