C++ Program to Convert Binary Number to Octal and vice-versa


In a computer system, the binary number is expressed in the binary numeral system while the octal number is in the octal numeral system. The binary number is in base 2 while the octal number is in base 8.

Examples of binary numbers and their corresponding octal numbers are as follows −

Binary NumberOctal Number
0101012
001117
1100131
1000020

A program that converts the binary numbers into octal and the octal numbers into binary is given as follows −

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int BinarytoOctal(int binaryNum) {
   int octalNum = 0, decimalNum = 0, count = 0;

   while(binaryNum != 0) {
      decimalNum += (binaryNum%10) * pow(2,count);
      ++count;
      binaryNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      octalNum += (decimalNum % 8) * count;
      decimalNum /= 8;
      count *= 10;
   }
   return octalNum;
}
int OctalToBinary(int octalNum) {
   int decimalNum = 0, binaryNum = 0, count = 0;
   while(octalNum != 0) {
      decimalNum += (octalNum%10) * pow(8,count);
      ++count;
      octalNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      binaryNum += (decimalNum % 2) * count;
      decimalNum /= 2;
      count *= 10;
   }
   return binaryNum;
}
int main() {
   int binaryNum = 1011, octalNum = 25;
   cout <<"Binary to Octal"<<endl;
   cout<<"Binary number: "<<binaryNum<<endl;
   cout<<"Octal number: "<<BinarytoOctal(binaryNum)<<endl;

   cout <<"Octal to Binary"<<endl;
   cout<<"Octal number: "<<octalNum<<endl;
   cout<<"Binary number: "<<OctalToBinary(octalNum)<<endl;
   return 0;
}

Output

The output of the above program is as follows −

Binary to Octal
Binary number: 1011
Octal number: 13
Octal to Binary
Octal number: 25
Binary number: 10101

In the above program, there are two functions BinaryToOctal() and OctalToBinary().

BinaryToOctal() converts the given binary number into an octal number. This is done by first converting the binary number into a decimal number and then converting the decimal number into an octal number. This is seen in the following code snippet −

int BinaryToOctal(int binaryNum) {
   int octalNum = 0, decimalNum = 0, count = 0;
   while(binaryNum != 0) {
      decimalNum += (binaryNum%10) * pow(2,count);
      ++count;
      binaryNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      octalNum += (decimalNum % 8) * count;
      decimalNum /= 8;
      count *= 10;
   }
   return octalNum;
}

OctalToBinary() converts the given octal number into a binary number This is done by first converting the octal number into a decimal number and then converting the decimal number into an binary number. This is seen in the following code snippet −

int OctalToBinary(int octalNum) {
   int decimalNum = 0, binaryNum = 0, count = 0;

   while(octalNum != 0) {
      decimalNum += (octalNum%10) * pow(8,count);
      ++count;
      octalNum/=10;
   }
   count = 1;
   while (decimalNum != 0) {
      binaryNum += (decimalNum % 2) * count;
      decimalNum /= 2;
      count *= 10;
   }
   return binaryNum;
}

In the function main(), the binary number and the octal number are given. Then their corresponding octal and binary numbers are calculated by calling BinaryToOctal() and OctalToBinary() respectively. This is shown below −

int main() {
   int binaryNum = 1011, octalNum = 25;
   cout <<"Binary to Octal"<<endl;
   cout<<"Binary number: "<<binaryNum<<endl;
   cout<<"Octal number: "<<BinarytoOctal(binaryNum)<<endl;

   cout <<"Octal to Binary"<<endl;
   cout<<"Octal number: "<<octalNum<<endl;
   cout<<"Binary number: "<<OctalToBinary(octalNum)<<endl;
   return 0;
}

Updated on: 25-Jun-2020

630 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements