In this tutorial, we will write a program to convert an octal number into binary in C++. Before that, you must have knowledge of the following topics in C++.
Binary number
The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.
Octal number
The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.
Let us go through a program for the Octal to Binary Conversion in C++.
C++ Program to Convert Octal to Binary
#include <iostream>
#include <cmath>
using namespace std;
//user defined function
int conversionFunc(int octNumber)
{
int deciNum = 0, binNumber = 0, i = 0;
while (octNumber != 0)
{
deciNum += (octNumber % 10) *pow(8, i);
++i;
octNumber /= 10;
}
i = 1;
while (deciNum != 0)
{
binNumber += (deciNum % 2) *i;
deciNum /= 2;
i *= 10;
}
return binNumber;
}
//Main function
int main()
{
int octNumber, result;
cout << "Enter an octal number: ";
cin >> octNumber;
result = conversionFunc(octNumber);
cout << "Equivalent Binary Number: " << result;
return 0;
}
Output:
Enter an octal number: 64
Equivalent Binary Number: 110100
You may go through the vice versa conversion: