Haskell Program to Count Number of Digits in an Integer


In this tutorial, we discuss writing a program to count the number of digits in an integer in the Haskell programming language.

In this tutorial, we see three ways to implement a program to count digits in Haskell.

  • Program to count the number of digits in an integer.
  • Program to count the number of digits in an integer using if-else.
  • Program to count the number of digits in an integer using the length function.

Algorithmic Steps

Take input or initialize a variable for an integer.

Implement the program logic to count the digits in an integer.

Print or display the count.

Example 1

Program to count the number of digits in an integer

-- function declaration
countDigits :: Int->Int
-- function definition
-- base case
countDigits 0 = 0
countDigits n = 1 + countDigits (div n 10)
main :: IO()
main = do
-- declaring and initializing a variable number
   let number = 123
-- invoking the function countDigits and printing the returned integer
   print ("number of digits in the number " ++ (show number) ++ " is:")
   print (countDigits number)

Output

"number of digits in the number 123 is:"
3

In the above program, we declared a function countDigits as such it takes an integer as an argument and returns an integer. In its function definition, we are accepting an integer argument n and returning 1 addition with a recursive call to itself with argument n divided with 10. The calls are invoked until the base case is attained in which the argument is 0. In this case, the function returns 0. I.e this function counts the number of digits in an integer. In the main function, we declared and initialized a variable number. Finally, we invoked the function countDigits and printed the returned output using the print function.

Example 2

Program to count the number of digits in an integer using if-else.

-- function declaration
countDigits :: Int->Int
-- function definition
countDigits n = if (n>0)
   then 1 + countDigits (div n 10)
   else 0
main :: IO()
main = do
-- declaring and initializing a variable number
   let number = 123
-- invoking the function countDigits and printing the returned integer
   print ("number of digits in the number " ++ (show number) ++ " is:")
   print (countDigits number)

Output

"number of digits in the number 123 is:"
3

In the above program, we declared a function countDigits as such it takes an integer as an argument and returns an integer. In its function definition, we are taking an integer argument n. If the value of n is greater than 0, return a recursive call to itself with argument n divided by 10 added with 1. If the value is zero we are returning zero. I.e this function counts the number of digits in an integer. In the main function, we declared and initialized a variable number. Finally, we invoked the function countDigits and printed the returned output using the print function.

Example 3

Program to count the number of digits in an integer using the length function.

-- function declaration
countDigits :: Int->Int
-- function definition
countDigits n = d
   where
      str = show n
      d = length str
main :: IO()
main = do
-- declaring and initializing a variable number
   let number = 123
-- invoking the function countDigits and printing the returned integer
   print ("number of digits in the number " ++ (show number) ++ " is:")
   print (countDigits number)

Output

"number of digits in the number 123 is:"
3

In the above program, we declared a function countDigits as such it takes an integer as argument and returns an integer. In its function definition, we are taking an integer argument n. We are parsing the number n into a string using the function show and loading it into a variable str. We invoked the function length with argument str and loaded the returned output into a variable d which is returned by the function. The function length is a list function that returns the count of elements in a list. As the strings in Haskell are lists of characters, all list functions can be used on strings. I.e this function counts the number of digits in an integer. In the main function, we declared and initialized a variable number. Finally, we invoked the function countDigits and printed the returned output using the print function.

Conclusion

In this tutorial, we discussed three ways to implement a program to count the number of digits in the Haskell programming language.

Updated on: 14-Dec-2022

809 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements