
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Number | Octal Number |
---|---|
01010 | 12 |
00111 | 7 |
11001 | 31 |
10000 | 20 |
A program that converts the binary numbers into octal and the octal numbers into binary is given as follows −
Example
#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; }
- Related Articles
- C++ Program to convert Octal Number to Decimal and vice-versa
- C++ Program to Convert Binary Number to Decimal and vice-versa
- How to Convert Decimal Number to Binary/Octal/Hex Number or Vice Versa in Excel?
- C++ Program to Convert Octal Number to Binary Number
- Binary to decimal and vice-versa in Python
- Golang program to convert the arraylist into string and vice-versa
- C++ Program to Convert Km/hr to miles/hr and vice versa
- How to Convert Letter to Number or Vice Versa in Excel?
- JAVA Program to Convert Binary to Octal
- JAVA Program to Convert Octal to Binary
- Golang Program to convert Binary to Octal
- Haskell Program to convert Binary to Octal
- Java Program to Convert the LinkedList into an Array and vice versa
- Java Program to Convert the ArrayList into a string and vice versa
- Convert string to DateTime and vice-versa in Python
