- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Haskell Program To Check Whether The Input String Is A Palindrome
- Haskell program to check whether the input number is a Prime number
- Haskell Program to Check whether the input number is a Neon Number
- Haskell Program to Check Palindrome
- C++ Program to Check Whether a Number is Palindrome or Not
- Java Program to Check whether the input number is a Neon Number
- Swift Program to Check whether the input number is a Neon Number
- Haskell Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Positive or Negative
- Haskell Program to Check Whether a Number is Prime or Not
- Write a Golang program to check whether a given number is a palindrome or not
- 8085 program to check whether the given 16 bit number is palindrome or not
- Bash program to check if the Number is a Palindrome?
- Python program to check whether the string is Symmetrical or Palindrome
- Python Program to Check whether a Singly Linked List is a Palindrome
