
- 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 Octal Number to Binary Number
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 |
---|---|
01101 | 15 |
00101 | 5 |
10110 | 26 |
01010 | 12 |
A program that converts the octal numbers into binary is given as follows −
Example
#include <iostream> #include <cmath> using namespace std; 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 octalNum = 33; 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 −
Octal to Binary Octal number: 33 Binary number: 11011
In the given program, the function 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 octal number is given. Then its corresponding binary number is calculated by calling OctalToBinary(). This is shown below −
int main() { int octalNum = 33; cout <<"Octal to Binary"<<endl; cout<<"Octal number: "<<octalNum<<endl; cout<<"Binary number: "<<OctalToBinary(octalNum)<<endl; return 0; }
- Related Articles
- C++ Program to Convert Binary Number to Octal and vice-versa
- Java Program to convert octal number to decimal number
- C# program to convert decimal to Octal number
- 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 decimal number to octal value
- Java program to convert float decimal to Octal number
- Java Program to convert decimal integer to octal number
- Python program to convert float decimal to octal number
- How to Convert Decimal Number to Binary/Octal/Hex Number or Vice Versa in Excel?
- Convert octal number to decimal number in Java
- Java Program to convert binary number to decimal number
- C++ Program to convert Octal Number to Decimal and vice-versa
