# 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.

### Example

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.

### Algorithm steps

• 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.

### Syntax

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

153


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 −

[1,2,3,4,5,6,7,8,9,153,370,371,407]


### Overall Program

Example

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)


### Output

[1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474]


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.

## Conclusion

In this tutorial, We discussed writing a program to check the Armstrong number between two Integers in Haskell Programming Language.

Updated on: 27-Oct-2022

193 Views 