Haskell Program to Check Armstrong Number


In Haskell we can check whether a given number is Armstrong or not using list comprehension and sum function. Armstrong numbers, also known as narcissistic numbers, are numbers such that the sum of the cubes of their digits is equal to the number itself.

For example, the number 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153.

Algorithm

  • Step 1 − The armstrong function calculates the sum of the cubes of the digits of n using list comprehension and the sum function, and define

  • Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. The main function takes input from the user and passes it to the armstrong function to check if it is an Armstrong number. If it is, a message "It is an Armstrong number." is displayed, otherwise, "It is not an Armstrong number." is displayed.

  • Step 3 − The variable named, “num” is being initialized. It will hold the number which is to be checked whether it is armstrong number or not.

  • Step 4 − The result is printed to the console using ‘putStrLn’ statement after the function is called.

Example 1

In this example, the armstrong function takes an integer n as input and returns True if it is an Armstrong number, and False otherwise.

The armstrong function calculates the sum of the cubes of the digits of n using list comprehension and the sum function.

armstrong :: Int -> Bool
armstrong n = sum [x^length (show n) | x <- (map (\x -> read [x]) (show n))] == n

main :: IO ()
main = do
   let num = 153
   if armstrong num
      then putStrLn "It is an Armstrong number."
      else putStrLn "It is not an Armstrong number."

Output

It is an Armstrong number.

Example 2

In this example, a function armstrong is defined using sum and digitToInt function to check if the passed number is armstrong number or not.

import Data.Char

armstrong :: Int -> Bool
armstrong n = n == sum [digitToInt x ^ length (show n) | x <- show n]

main :: IO ()
main = do
   let num = 153
   if armstrong num
      then putStrLn "It is an Armstrong number."
      else putStrLn "It is not an Armstrong number."

Output

It is an Armstrong number.

Example 3

In this example, the armstrong function takes an integer n as input and returns True if it is an Armstrong number, and False otherwise.

The armstrong function calculates the sum of the cubes of the digits of n using list comprehension and the sum function.

armstrong :: Int -> Bool
armstrong n = n == sum [x^d | x <- digits n, let d = numberOfDigits n]
   where
      digits n = map (\x -> read [x]) (show n)
      numberOfDigits n = length (show n)

main :: IO ()
main = do
   let num = 153
   if armstrong num
      then putStrLn "It is an Armstrong number."
      else putStrLn "It is not an Armstrong number."

Output

It is an Armstrong number.

Conclusion

In Haskell, we can write a program to check if a given number is an Armstrong number by using functions such as show, map, read, length, and sum. The basic idea is to convert the number to a string, then convert each character in the string back to an integer, and finally calculate the sum of the cubes of the digits. If the sum is equal to the original number, it's an Armstrong number, otherwise it's not.

Updated on: 25-Apr-2023

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements