- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Haskell program to convert a decimal number into a binary number

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.

- Related Articles
- Haskell Program to convert the decimal number to binary using recursion
- Haskell Program to convert a number into a complex number
- Java Program to convert binary number to decimal number
- C++ program to Convert a Decimal Number to Binary Number using Stacks
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- C++ Program To Convert Decimal Number to Binary
- Python program to convert decimal to binary number
- Convert decimal to binary number in Python program
- Java program to convert decimal number to binary value
- Java program to convert binary number to decimal value
- Haskell Program to convert the string into a floating-point number
- Write a Golang program to convert a binary number to its decimal form
- Write a Golang program to convert a decimal number to its binary form
- C++ Program to Convert Binary Number to Decimal and vice-versa