# Haskell Program to convert Decimal to Binary

In Haskell, we will convert Decimal to Binary by using reverse function , tail recursion, and divMod function. In the first example, we are going to use (decToBin n | n < 0 = error "Input must be non-negative" | n == 0 = "0" | otherwise = reverse $decToBin' n) and in the second example, we are going to use (binDigits 0 = "" and binDigits n = let (q, r) = n divMod 2 in show r ++ binDigits q) function. ## Method 1: Using reverse function and tail recursion In this method, the decToBin function takes an integer n as input and returns a binary representation of n as a string. The function first checks if the input n is non-negative. If n is negative, it raises an error. If n is zero, it returns the string "0". Otherwise, it calls the decToBin' helper function with n, and then reverses the resulting string. ### Algorithm • Step 1 − The Data.Char library is imported. • Step 2 − The decToBin function is defined using reverse function as, decToBin n | n < 0 = error "Input must be non-negative" | n == 0 = "0" | otherwise = reverse$ decToBin' n.

The helper decToBin’ function is defined as,
decToBin' n
| n == 0    = ""
| otherwise = show (n mod 2) ++ decToBin' (n div 2).

• Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the decToBin function is called with the argument and the result is printed to the console.

• Step 4 − The variable named, “n” is being initialized. It will hold the decimal number that is to be converted to respective binary number.

• Step 5 − The resultant binary number is printed to the console using ‘putStrLn’ statement after the function is called.

### Example 1

In this example, the tail recursive function is defined using reverse function to convert the decimal to binary number.

decToBin :: Integer -> String
decToBin n
| n < 0     = error "Input must be non-negative"
| n == 0    = "0"
| otherwise = reverse $decToBin' n decToBin' :: Integer -> String decToBin' n | n == 0 = "" | otherwise = show (n mod 2) ++ decToBin' (n div 2) main :: IO () main = do let n = 15 putStrLn$ "The binary representation of " ++ show n ++ " is " ++ decToBin n


### Output

The binary representation of 15 is 1111


## Method 2: Using divMod function

In this method, a helper function binDigits is used to recursively generate the binary digits of the input number. The function binDigits takes an input number n and returns the binary digits of n as a string. The digits are generated by repeatedly dividing n by 2 and taking the remainder. The result is accumulated as a string in reverse order, using show r to convert the remainder to a string.

### Algorithm

• Step 1 − The decToBin function is defined using reverse function as,

decToBin n
| n < 0     = error "Input must be non-negative"
| n == 0    = "0"
| otherwise = reverse $binDigits n. The recursive binDigits function is defined using divMod function as, binDigits 0 = "" binDigits n = let (q, r) = n divMod 2 in show r ++ binDigits q.  • 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. In the main function, the decToBin function is called with the argument and the result is printed to the console. • Step 3 − The variable named, “n” is being initialized. It will hold the decimal number that is to be converted to respective binary number. • Step 4 − The resultant binary number is printed to the console using ‘putStrLn’ statement after the function is called. ### Example 1 In this example, the recursive function is defined using divMod function to convert the decimal to binary number. decToBin :: Integer -> String decToBin n | n < 0 = error "Input must be non-negative" | n == 0 = "0" | otherwise = reverse$ binDigits n

binDigits :: Integer -> String
binDigits 0 = ""
binDigits n = let (q, r) = n divMod 2
in show r ++ binDigits q
main :: IO ()
main = do
let n = 15
putStrLn \$ "The binary representation of " ++ show n ++ " is " ++ decToBin n


### Output

The binary representation of 52 is 1111


## Conclusion

In Haskell, a decimal number is converted to binary number using reverse function along with tail recursion or by using divMod function. The process of converting a decimal number to binary involves repeatedly dividing the decimal number by 2 and taking the remainder. The remainders are then read in reverse order to obtain the binary equivalent of the decimal number.