- 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 Binary to Octal

In Haskell, we can use the user-defined function, Folder, reverse and helper function to convert binary to octal number. In the first example, we are going to use (binaryToOctal binary = showOct decimalValue "" where decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary) function. In the second example, we are going to use (binaryToOctal binary = showOct decimalValue "" where decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary octalDigits = reverse (octalHelper decimalValue) octalHelper n | n < 8 = [n] | otherwise = (n `mod` 8) : octalHelper (n `div` 8)).

### Algorithm

**Step 1**− The Data.Char and Numeric libraries are imported.**Step 2**− Define the binaryToOctal function**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.**Step 4**− The variable named, “binaryNumber” is being initialized. It will hold the binary that is to be converted to respective octal number.**Step 5**− The resultant octal is printed to the console using ‘putStrLn’ statement after the function is called.

### Example 1

In this example, the function is defined using user-defined binaryToOctal function with the help of foldl function to convert the binary to octal number.

import Data.Char (digitToInt) import Numeric (showOct) binaryToOctal :: String -> String binaryToOctal binary = showOct decimalValue "" where decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary main :: IO () main = do let binaryNumber = "110101010" putStrLn ("Binary number: " ++ binaryNumber) putStrLn ("Octal number: " ++ binaryToOctal binaryNumber)

### Output

Binary number: 110101010 Octal number: 652

### Example 2

In this example, the function is defined using user-defined binaryToOctal function with the help of foldl and reverse function along with helper function to convert the binary to octal number.

import Data.Char (digitToInt) import Numeric (showOct) binaryToOctal :: String -> String binaryToOctal binary = showOct decimalValue "" where decimalValue = foldl (\acc x -> acc * 2 + digitToInt x) 0 binary octalDigits = reverse (octalHelper decimalValue) octalHelper n | n < 8 = [n] | otherwise = (n `mod` 8) : octalHelper (n `div` 8) main :: IO () main = do let binaryNumber = "110101010" putStrLn ("Binary number: " ++ binaryNumber) putStrLn ("Octal number: " ++ binaryToOctal binaryNumber)

### Output

Binary number: 110101010 Octal number: 652

## Conclusion

Binary to octal conversion is the process of converting a binary number (base 2) to its octal equivalent (base 8). To do this conversion, we need to group the binary digits into sets of three, starting from the rightmost digit, and then convert each set into its equivalent octal digit. In Haskell, a binary number is converted to octal using user-defined function along with foldl and reverse function. Also, we can use helper function with the user-defined function for conversion.

- Related Articles
- Haskell Program to convert Decimal to Octal
- Golang Program to convert Binary to Octal
- JAVA Program to Convert Binary to Octal
- JAVA Program to Convert Octal to Binary
- C++ Program to Convert Octal Number to Binary Number
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- How to Convert Binary to Octal?
- How to Convert Octal to Binary?\n
- C++ Program to Convert Binary Number to Octal and vice-versa
- Haskell Program to convert the decimal number to binary using recursion
- Haskell Program to convert the Binary number to Gray code using recursion
- Java Program to convert integer to octal
- Java Program to convert Decimal to Octal
- JAVA Program to Convert Octal to Hexadecimal