The problem is to convert the given binary number (represented as string) to its equivalent octal number. The input could be very large and may not fit even into unsigned long long int.
Input : 110001110 Output : 616 Input : 1111001010010100001.010110110011011 Output : 1712241.26633
The idea is to consider the binary input as a string of characters and then follow the steps:
- Get length of substring to the left and right of the decimal point(‘.’) as left_len and right_len.
- If left_len is not a multiple of 3 add min number of 0’s in the beginning to make length of left substring a multiple of 3.
- If right_len is not a multiple of 3 add min number of 0’s in the end to make length of right substring a multiple of 3.
- Now, from the left extract one by one substrings of length 3 and add its corresponding octal code to the result.
- If in between a decimal(‘.’) is encountered then add it to the result.
Octal number = 1712241.26633
Time Complexity: O(n), where n is the length of string.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to Convert Octal Number to Binary Number
- Program to Convert Hexadecimal to Octal
- Octal equivalents of connected components in Binary valued graph
- Check if Decimal representation of an Octal number is divisible by 7
- Largest N digit Octal number which is a Perfect square
- Program for Octal to Decimal Conversion
- Decimal to octal conversion with minimum use of arithmetic operators
- Program for Decimal to Octal Conversion
- Convert a binary number to hexadecimal number
- Convert decimal fraction to binary number
- Python program to convert decimal to binary number
- Python Program to Convert any Positive Real Number to Binary string
- Python program to convert floating to binary
- Minimum swaps required to convert one binary string to another
- Convert numbers into binary representation and add them without carry
- Convert Binary fraction to Decimal
- Convert the given BCD to its equivalent Binary form
- Minimize cost to convert all characters of a binary string to 0s
- Program to Convert BCD number into Decimal number
- Gray to Binary and Binary to Gray conversion
Improved By : Atul_kumar_Shrivastava