# C++ Program for Binary to Octal conversion

## Binary to Octal conversion in C++

Here, we will discuss binary to octal conversion in C++.There are two approaches of converting an octal number into a binary number like

• grouping the binary number into pairs of three and converting into octal.
• By converting the binary number into a decimal number first and then convert that decimal number into an octal number.

We are using second approach, this approach is long but it is very easy to implement and also the chances of error are also very minimum.

## Method 1:-

We will first convert the Binary number into Decimal and then convert the Decimal number into Octal

Make sure that you have gone through these approaches below –

## Method 1 Code

Run
```#include<iostream>
#include<math.h>
using namespace std;

// function to convert binary to octal
void convert(long long num)
{
int octalDigit = 0, count = 1, i = 0, pos = 0;
int octalArray[32] = {0};

while(num != 0)
{
int digit = num % 10;

octalDigit += digit * pow(2, i);
i++;
num /= 10;

// placing current octalsum for 3 pair in array index position
octalArray[pos] = octalDigit;

// whenever we have read next 3 digits
// setting values to default
// increasing pos so next values can be placed at next array index
if(count % 3 == 0)
{
octalDigit = 0;
i = 0;
pos++;
}
count++;
}

// printing octal array in reverse order
for (int j = pos; j >= 0; j--)
cout<<octalArray[j];

}

//main program
int main()
{
// long used rather than int to store large values
long long binary;

printf("Enter binary number: ");
scanf("%lld", &binary);

convert(binary);

return 0;
}```

### Output :

`Enter binary number: 1010Decimal : 10Octal : 12`

## Method 2 Code

Run
```#include<iostream>
#include<math.h>
using namespace std;

// function to convert binary to octal
void convert(long long num)
{
int octalDigit = 0, count = 1, i = 0, pos = 0;
int octalArray[32] = {0};

while(num != 0)
{
int digit = num % 10;

octalDigit += digit * pow(2, i);
i++;
num /= 10;

// placing current octalsum for 3 pair in array index position
octalArray[pos] = octalDigit;

// whenever we have read next 3 digits
// setting values to default
// increasing pos so next values can be placed at next array index
if(count % 3 == 0)
{
octalDigit = 0;
i = 0;
pos++;
}
count++;
}

// printing octal array in reverse order
for (int j = pos; j >= 0; j--)
cout<<octalArray[j];

}

//main program
int main()
{
// long used rather than int to store large values
long long binary;

printf("Enter binary number: ");
scanf("%lld", &binary);

convert(binary);

return 0;
}```

### Output :

`Enter binary number: 01010125`

## Method 2:-

Method 2 uses the concept of grouping 3 successive digits/bits of the binary number and calculating octal digits against each grouping

We use an additional array to store the octal digits at each index.

We will need to print the array in reverse to get actual octal equivalent.

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### 2 comments on “C++ Program for Binary to Octal conversion”

• Y.PRAVEEN

//using if else updated one
#include
using namespace std;

int main() {
int num;
cout << "Enter a binery number" <> num;

if (num == 000) {
cout << "0";
}
else if (num == 001) {
cout << "1";
}
else if (num == 010 || num == 10) {
cout << "2";
}
else if (num == 011 || num == 11) {
cout << "3";
}
else if (num == 100) {
cout << "4";
}
else if (num == 101) {
cout << "5";
}
else if (num == 110) {
cout << "6";
}
else if (num == 111) {
cout << "7";
}
else {
cout << "Not an octal number" << endl;
cout << "Please enter binery number between 000 to 111" <0″);
return 0;
}

• Y.PRAVEEN

//using if else
#include
using namespace std;

int main() {
int num;
cout << "enter a binery number"<> num;

if ( num == 0) {
cout << "0";
}
else if ( num == 001 ) {
cout << "1";
}
else if (num == 010 || num == 10) {
cout << "2";
}
else if (num == 011 || num == 11) {
cout << "3";
}
else if (num == 100) {
cout << "4";
}
else if (num == 101) {
cout << "5";
}
else if (num == 110) {
cout << "6";
}
else {
cout <0″);
return 0;
}