Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C++ Program to Convert a Binary Number to Octal Number
3.
Examples 
3.1.
Example 1
3.2.
Example 2
4.
C++ Program To Convert Binary to Octal
5.
Frequently Asked Questions
5.1.
What is Time Complexity?
5.2.
Is it necessary to make a multiple of 3 only? Can we make multiples of 2 or 4 instead?
5.3.
What is the difference between the octal and hexadecimal number system?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

C++ Program to Convert Binary Number to Octal

Author SHIVAM SINHA
0 upvote

Introduction

We can easily convert a Binary number into octal; for that, we need to consider the binary number as a string of characters. Now the Binary number can be in decimal form as well, so for finding the octal number of a decimal binary number, there are certain steps that we have to follow. Let’s look at the approach to convert binary number to octal number.

C++ Program to Convert a Binary Number to Octal Number

Follow the below steps to convert binary number to octal after converting the number to string.

  • To convert a binary number into octal, we will consider the binary number as a string of characters. 
  • Then we will find out the length of the substring from the left of the decimal point and right of the decimal point. 
    • If the left substring is not a multiple of 3, then to make it a multiple of 3, we will be adding 0`s to the left of the substring. 
    • If the right substring is not a multiple of 3, then in order to make it a multiple of 3, we will be adding 0`s to the right of the substring. 
  • Since we know binary numbers have a base of 2. So, we will be using the 4 2 1 rule, as we can make any number between 0 to 7 using this rule. We will divide the string into parts of 3 as 4 2 1 are 3 digits. 
  • Now we will be traversing from 0 to the end of the binary number, which is set to n, and let i be the first binary number from the group of 3 numbers, so, for the ith bit, its corresponding value will be 4, if (i+1)th bit is set then add 2 and if (i+2)th bit is set then add 1. 
  • All this, we produce one bit of the octal number. For another number, we have to repeat the same steps.

Also Read - C++ Interview Questions And Application of Oops

Examples 

Examples 

Now let’s understand this with the help of a few examples, and let’s see their explanation.

Example 1

Input: 110001110

Output: 616

Suppose we have a Binary number, and we have to convert it to Octal Number, so considering it as a string, we have to group this in a multiple of 3, and if it is not in a multiple of 3, then we can make it by adding 0’s in the beginning.

                        

So the given binary digit is already multiple of 3. 

Now, let’s see all the possible combinations of 3-digit binary number and their respective values:

000 = 0

001 = 1

010 = 2

011 = 3

100 = 4

101 = 5

110 = 6

111 = 7

 

Considering the above table, let’s find the value of each group which is a multiple of 3 that we have created.

 

So, the octal number of binary number 110001110 is 616

Example 2

Let us take another example of a decimal number and convert it into octal number.

Input: 1111001010010100001.010110110011011

Output: 1712241.26633

So, we have a decimal number 1111001010010100001.010110110011011, and we have to convert it to an octal number.

Let’s solve it step-wise to understand this more clearly.

 

Step1: Find the length of the substring to the left and right of the decimal point(‘.’) .

So here,

left_substring=1111001010010100001

right_substring=010110110011011

 

Step 2: If left_substring is not a multiple of 3, then add min number of 0’s in the beginning to make the length of left substring a multiple of 3.

 

So here, left_substring is not a multiple of 3, so we have added two 0’s at the beginning(marked a red) so that left_substring becomes a multiple of 3. 

So, again, by considering the table that we created in the previous example

So, Octal Value for left_substring=1712241.

Step 3: If right_substring is not a multiple of 3, then add min number of 0’s in the end to make the length of right substring a multiple of 3.

 So, in this example, right_substring is a multiple of 3, so we don’t have to add 0 in the end. So, we have directly found the value corresponding to the table that we created in step 2

So, octal value of right_substring = 26633

 

Step 4:

The final step is to merge the left_substring and right _substring.

So the octal value of 1111001010010100001.010110110011011 is 1712241.26633

Also check out - Substr C++

C++ Program To Convert Binary to Octal

// C++ implementation to convert a binary number
// to octal number
#include <bits/stdc++.h>
using namespace std;
// function to create map between binary
// number and its equivalent octal
void mapping(unordered_map<string, char> *hash)
{
	(*hash)["000"] = '0';
	(*hash)["001"] = '1';
	(*hash)["010"] = '2';
	(*hash)["011"] = '3';
	(*hash)["100"] = '4';
	(*hash)["101"] = '5';
	(*hash)["110"] = '6';
	(*hash)["111"] = '7';
}//this is the table that we saw in examples


// Function to find octal equivalent of binary
string convertBinToOct(string bin)
{
	int left= bin.size(); //length of string before the decimal point
	int right= bin.find_first_of('.'); //length of string after the decimal point

	int len_left = right != -1 ? right : left;

	// adding 0's in the beginning to make it multiple of 3
	for (int i = 1; i <= (3 - len_left % 3) % 3; i++)
		bin = '0' + bin;

	// checking if decimal point exists or not
	if (right != -1)
	{
	// length of string after decimal
		int len_right = left - len_left - 1;

		// adding 0's in the end to make right it divisible by 3
		for (int i = 1; i <= (3 - len_right % 3) % 3; i++)
			bin = bin + '0';
	}

	unordered_map<string, char> bin_oct_map;
	mapping(&bin_oct_map);

	int i = 0;
	string ans = "";

	while (1)
	{

		//extract substring of size 3 and add its corresponding value
		ans += bin_oct_map[bin.substr(i, 3)];
		i += 3;
		if (i == bin.size())
			break;


		if (bin.at(i) == '.')
		{
			ans += '.';
			i++;
		}
	}
	return ans;
}


//main function
int main()
{
    cout << "Enter Binary Number: ";
    string n;
    cin >> n;
    cout << "Octal number: " << convertBinToOct(n);
    return 0;
}

 Input:

10110010

Output:

262
You can also try this code with Online C++ Compiler
Run Code

 

You can try by yourself with the help of online C++ Compiler.

Must read decimal to binary c++ and, Dynamic Binding in C++.

Frequently Asked Questions

What is Time Complexity?

The amount of time taken by an algorithm to run or execute. It shows the efficiency of an algorithm. To know more about the time complexity, click here.

Is it necessary to make a multiple of 3 only? Can we make multiples of 2 or 4 instead?

Yes, it is necessary to make multiples of 3 only; otherwise, it will give an incorrect result.

What is the difference between the octal and hexadecimal number system?

The octal number system is a base-8 number system and uses the digits 0 - 7 to represent numbers whereas the hexadecimal number system is a base-16 number system and uses the digits 0 - 9 along with the letters A - F to represent numbers.

Conclusion

In this article, we have discussed the C++ program to convert Decimal into octal. We also discussed a few examples with explanations through the figure. 

To learn more, see Basics of C++ with Data StructureDBMSOperating System by Coding Ninjas, and keep practising on our platform Coding Ninjas Studio.

If you think you are ready for the tech giants company, check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more!. You can also prepare for tech giants companies like Amazon, Microsoft, Uber, etc., by looking for the questions asked by them in recent interviews. If you want to prepare for placements, refer to the interview bundle. If you are nervous about your interviews, you can see interview experiences to get the ideas about questions that have been asked by these companies.

Do upvote if you find this blog helpful!

Be a Ninja

Happy Coding!

Live masterclass