- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- 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 armstrong number between two integers
This tutorial will discuss writing a program to display Armstrong numbers between two Integers. Haskell is a Functional, Declarative, and Strongly Typed Language. Computations in Haskell are Mathematical Functions.
Armstrong Number is a number that is equal to the sum of its raised to the power of the number of digits in that number.
9474 is an Armstrong Number because 9474 = 9^4 + 4^7 + 7^4 + 4^4.
153 is an Armstrong Number because 153 = 1^3 + 5^3 + 3^3.
Implement a function to check whether a number is an Armstrong number.
Implement a function to generate Armstrong numbers between two integers.
Display the generated Armstrong numbers
Program to Display all primes between two Integers
We break the program into simpler functions.
Function to count the number of digits in an integer -- function declaration cntDigits :: Int->Int -- function definition cntDigits 0 = 0 cntDigits n = 1 + cntDigits (div n 10)
Above is an utility function useful in finding the number of digits in an integer. We declared the function as It takes an integer as an argument and it returns an integer. In the function definition, We defined a base condition that returns zero if the argument is zero. In all other cases, the function returns 1 addition with a recursive call to itself with an argument number divided by 10. I.e function returns the numbers of digits in an integer.
Example: Output for cntDigits 123 is: 3
Function to sum the digits raised to the power of a number of digits -- function declaration sumDigits :: Int->Int->Int -- function definition sumDigits 0 _ = 0 sumDigits n digits= d^digits + sumDigits k digits where d = mod n 10 k = div n 10
Above function is utility function to return the sum of the digits raised to the power of “number of digits in the Integer”. We declared a function such that it takes two integers as arguments (number and digits in that number) and returns an Integer. In the function definition, we extracted the digits in the integer and returned them raised to the power of “number of digits in the Integer” recursively until it met the base case where the first argument is zero.
Note − “_” denotes a wild card pattern used to match any pattern.
Example − Output for sumDigits 153 is
Function to check whether a number is an Armstrong Number
-- function declaration isArmstrong :: Int->Bool -- function definition isArmstrong n = n==(sumDigits n (cntDigits n))
Above function is an utility function to check where a number is an Armstrong Number.We declared the function as It takes an integer to be checked as input and returns a Boolean. In the function definition, we are comparing the argument number with the output of the invoked utility function sumDigits whose arguments are input Integer, and the number of digits in that integer that is returned through the utility function cntDigits, i.e This function returns true if the passed integer argument is an Armstrong else it returns false.
Example: Output for isArmstrong 153 is: True
Function to display Armstrong number between Two Integers
-- function declaration displayArmstrong :: Int->Int->[Int] -- function definition displayArmstrong a b = [x | x<-[a..b], isArmstrong x]
Above is an utility function to display all the Armstrong numbers in a range. We declared the function as such it takes two integers as arguments and returns a list of integers. In the function definition, we use list comprehension which generates the numbers in a range between two arguments and returns the numbers which are Armstrong by checking them using the function isArmstrong.
For example output for display 1 500 is −
cntDigits :: Int->Int cntDigits 0 = 0 cntDigits n = 1 + cntDigits (div n 10) sumDigits :: Int->Int->Int sumDigits 0 _ = 0 sumDigits n digits= d^digits + sumDigits k digits where d = mod n 10 k = div n 10 isArmstrong :: Int->Bool isArmstrong n = n==(sumDigits n (cntDigits n)) displayArmstrong :: Int->Int->[Int] displayArmstrong a b = [x | x<-[a..b], isArmstrong x] main :: IO() main = do -- declaring initializing the number to be let a = 1 let b = 1000 print (displayArmstrong a b)
Above is the overall program to display all the Armstrong numbers between two integers. In the main function of the above program, we declared and initialized two variables a and b. We invoked the function displayArmstrong with arguments a and b which returns all the Armstrong numbers in between the two integers. Finally, we are printing the returned list of Armstrong numbers returned.
In this tutorial, We discussed writing a program to check the Armstrong number between two Integers in Haskell Programming Language.
Kickstart Your Career
Get certified by completing the courseGet Started