Convert Decimal Number to Binary Number in Haskell



This tutorial discusses writing a program to convert a decimal number into a binary number in the Haskell programming language. Haskell is a Declarative, Strongly Typed, and Functional programming Language. The computations in Haskell are mathematical functions.

In a Decimal number system, each number is denoted with numbers ranging from 0-9. This number system is also called as base 10 number system. For example, The number four hundred and ninety-one is represented as 491(4*10^2 + 9*10^1 + 1*10^0).

In a Binary number system, each number is represented as numbers ranging from 0-1. This number system is also called as base 2 number system. For example, The number ninety-one is represented as 1011011(1*2^6 + 0*2^5 + 1*2^4 + 1*2^3 + 0*2^2 + 1*2^1 + 1*2^0).

Algorithm Steps

  • Declare function toBinary to convert the decimal number into a binary form representing a decimal number.

  • Create the main function and assign a Decimal value that needs to be converted to binary.

  • Using the toBinary function, convert the decimal number

  • Print the final output

Example

Program to convert a decimal number into a binary number

-- function declaration
toBinary :: Int->[Char]

-- function definition
-- base case-1
toBinary 0 = []
-- base case-2
toBinary 1 = show 1
toBinary n = toBinary n1 ++ d
   where
   r = mod n 2
   d = show r
   n1 = div n 2

main :: IO()
main = do
-- initializing a variable number
   let number = 91
-- invoking the function toBinary
   let binNumber = toBinary number
   print ("The binary form of the decimal number "++ show number ++ " is:")
   print (binNumber)

Output

"The binary form of the decimal number 91 is:"
"1011011"

Description

  • We declared a function toBinary as such it takes an integer as an argument and returns a string(list of characters). Its function definition accepts an integer n as an argument and returns a recursive call to itself with argument n1 concatenated with a variable d. The values of n1 and d are computed in the following lines. Where keywords in Haskell can be used to split complex logic.

  • The reminder from the division of number n with 2 is computed using the mod function and loaded into a variable r. The integer value r is converted into a string using the function show and the returned string value is loaded into a variable d.

  • The quotient from the division of number n with 2 is computed using the function div and loaded into a variable n1.

  • The function toBinary converts the decimal number into string form representing a decimal number.

  • In the main function, a variable number is initialized with a decimal number 91. The function toBinary is invoked with a variable number as an argument and the returned binary string is loaded into a variable binNumber. Finally, the output is printed using the print function.

Conclusion

This tutorial discussed implementing a program to convert a decimal number into a binary number in the Haskell programming language.

Updated on: 2022-12-15T11:33:48+05:30

712 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements