Binary Numbers
The binary number system is made up of two different numerals, zero and one. All numbers can be represented using 0 and 1. In fact, our computer only understands binary numbers. They have a base of 2.
Example: The number 567 in decimal corresponds to 1000110111 in binary.
The logic for Converting Octal Numbers to Binary
Before we move on to discuss the code implementation of converting an octal number into a binary number, let us first discuss the logic that we shall use in the code while converting octal to binary.
- We simply create a blank variable having a String data type.
- We iterate through each of the digits and find the binary equivalent of that digit. For e.g., the binary equivalent of 4 is 100, the binary equivalent of 2 is 010, etc.
- We append the binary equivalent of every digit to the string variable which we created.
- After iterating over every digit, the result is stored in the string variable, and we return/print the value inside the string variable.
The below image demonstrates how the octal number is converted into binary.
We have an octal number 567. In order to convert it into binary, we consider each of the digits separately. Convert each of the digits into its binary form and append it to the output string.
The binary value of 5 is 101. The binary value of 6 is 110, and that of 7 is 111. Therefore the binary value of octal number 567 is the three binary values combined, i.e., 101110111.
Also Read - C++ Interview Questions
C++ Program to Convert Octal Number to Binary
Input: An octal number consisting of digits from 0 to 7.
Expected output: The binary form of the octal number provided in the input.
Code
// C++ program to convert Octal number to Binary
#include <iostream>
using namespace std;
// function to get the binary representation of
// numbers from 0 to 7
string binaryRepresentation(char digit){
if(digit=='0'){
return "000";
}
else if(digit=='1'){
return "001";
}
else if(digit=='2'){
return "010";
}
else if(digit=='3'){
return "011";
}
else if(digit=='4'){
return "100";
}
else if(digit=='5'){
return "101";
}
else if(digit=='6'){
return "110";
}
else{
return "111";
}
}
// Function to convert an octal number into binary
string octalToBinary(string octal)
{
int len = octal.length();
string binary = "";
for(int i=0;i<len;i++){
// for every character append its binary representation in the result
binary+=binaryRepresentation(octal[i]);
}
return binary;
}
// Driver code
int main()
{
string octal;
cout<<"Enter an Octal number:\n";
cin>>octal;
// Convert Octal to Binary
cout << "Binary representation of the octal number is = "
<< octalToBinary(octal);
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Input
567
You can also try this code with Online C++ Compiler
Run Code
Output
Enter an Octal number:
567
The binary representation of the octal number is = 101110111
You can also try this code with Online C++ Compiler
Run Code
Time Complexity
The time complexity of the above code is O(n), where n is the number of characters in the input string of the octal number. It is because the for-loop runs for the number of times equal to the number of characters in the input string.
Space Complexity
The space complexity of the above code is O(1) because there is no extra space used in the code.
Must read decimal to binary c++ And Application of Oops
Frequently Asked Questions
What is an Octal number?
The octal number system is made up of eight different numerals, from 0 to 7. The octal numbers have a base of 8.
What is a binary number?
The binary number system is made up of two different numerals, zero and one. All numbers can be represented using 0 and 1.
How can we convert an octal number into binary?
In order to convert it into binary, we consider each of the digits separately. We Convert each of the digits into its binary form and append it to the output string.
What is the binary form of the octal number 764?
111110100
Conclusion
In this article, we have discussed what Octal numbers and Binary numbers are and the C++ code to convert octal numbers to binary numbers. You should definitely read this blog to get a more clear understanding of number systems.
Do not forget to refer to our guided paths for complete interview preparation guidance. Check out this amazing Data Structures and Algorithms course and enhance your DSA skills. You can also try the Competitive Programming course and upskill your CP skills.
Check out the free mock test series to test your knowledge. If you are preparing for tech interviews, you should definitely check out the problems, interview experiences, and interview bundle for placement preparations.
Do upvote our blog if you find them helpful and engaging!
Happy Learning!