# Octal to Binary Conversion

Octal to Binary Conversion” is one of the classic programming problem exercise for beginners. Here, we are given an octal number, entered by user and our task is to write a program to convert the given octal number to its equivalent binary number.

Example:

```Octal = 65
Binary = 110101
Octal = 78
Binary = 11110000```

The steps for octal to binary conversion are as follows:

1. Scan the octal number from left to right.
2. Convert each digit of octal number into corresponding 3-digit binary number.
3. Combine all the 3-digit binary number to get the result.
4. Truncate the front 0s of the binary number, if exist.
5. Print the result.

#### C++ Program for octal to binary conversion is as follows:

```/* C++ Program to Convert Octal Number to Binary Number */
#include<bits/stdc++.h>
using namespace std;
int main()
{
/* Scan the Octal Number */
int Octal;
cout<<"Enter the Octal Number: ";
cin>>Octal;

/* Converting Octal to Binary*/
string Binary;
int temp = Octal;
while(temp != 0)
{
int remainder = temp % 10;
string val;

/* Binary equivalent of each digit */
switch(remainder) {
case 0: val = "000";
break;
case 1: val = "001";
break;
case 2: val = "010";
break;
case 3: val = "011";
break;
case 4: val = "100";
break;
case 5: val = "101";
break;
case 6: val = "110";
break;
case 7: val = "111";
break;
case 8: val = "1000";
break;
case 9: val = "1001";
break;
}

Binary = val + Binary ;
temp = temp / 10;
}

/*Deleting the extra zeros from front*/
while(Binary!='1')
{
Binary.erase(Binary.begin()+0);
}

/*Printing the result */
cout<<"The Binary of "<<Octal<<" is "<<Binary;
}
```
```OUTPUT:
Enter the Octal Number: 78
The Binary of 78 is 1111000```

Related Posts: