- C++ Programming Examples
- C++ Programming Examples
- C++ Print Hello World
- C++ Get Input from User
- C++ Print Integer
- C++ Add Two Numbers
- C++ Check Even or Odd
- C++ Check Prime or Not
- C++ Check Alphabet or Not
- C++ Check Vowel or Not
- C++ Check Leap Year or Not
- Check Reverse equal Original
- C++ Add Subtract Multiply Divide
- C++ Make Calculator
- C++ Add Digits of Number
- C++ Find Average, Percentage
- C++ Calculate Arithmetic Mean
- C++ Calculate Student Grade
- C++ Print Multiplication Table
- C++ Print Prime Numbers
- C++ Add n Numbers
- C++ Interchange Digits of Number
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Count Positive Negative Zero
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Find Factorial of Number
- C++ Find HCF & LCM
- C++ Area, Perimeter of Square
- C++ Area, Perimeter of Rectangle
- C++ Area, Perimeter of Triangle
- C++ Area, Circumference of Circle
- C++ Fahrenheit to Celsius
- C++ Celsius to Fahrenheit
- C++ Print ASCII Value
- C++ Print Fibonacci Series
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Decimal to Binary
- C++ Decimal to Octal
- C++ Decimal to Hexadecimal
- C++ Binary to Decimal
- C++ Binary to Octal
- C++ Binary to Hexadecimal
- C++ Octal to Decimal
- C++ Octal to Binary
- C++ Octal to Hexadecimal
- C++ Hexadecimal to Decimal
- C++ Hexadecimal to Binary
- C++ Hexadecimal to Octal
- C++ Pattern Programs
- C++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Print Smiling Face
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- Add Two Numbers using Pointer
- Find Largest Element in Array
- Find Smallest Element in Array
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- C++ Merge two Arrays
- C++ Bubble Sort
- C++ Selection Sort
- C++ Insertion Sort
- C++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array Programs
- C++ Print String
- C++ Find Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Generate Random Numbers
- C++ Read a File
- C++ Write Content to File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Print Date/Time
- C++ Get IP Address
- C++ Shutdown, Restart Computer
- C++ Frequency of Word
- C++ Add Binary Numbers
- C++ Append Data in File
- C++ Find Discounted Price
- C++ Find Shipping Charge
- C++ Calculate Simple Interest
- C++ Find Telephone Bills
- C++ Find Wage of Labor
- C++ Capitalize All Words in File
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Divisibility Test
- C++ Check Perfect Number
- C++ Common Elements
- C++ Days into Years, Months
- Count Characters, Words in File
- C++ Count Digits in Number
- C++ Count Even/Odd Numbers
- C++ Get Numbers from String
- C++ Find Factors of Number
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Sum of First & Last Digit
- C++ Sum of n Natural Numbers
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Product of Digits of Number
- C++ Sum of Even/Odd Numbers
- C++ Sum of Squares of Digits
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List

« Previous Program Next Program »

In this article, you will learn and get code on octal to binary conversion in C++. The first program is created that actually converts and then prints binary equivalent of given octal number. And the second program just prints the binary equivalent of each and every octal digit one by one.

Before going through these programs, if you're not aware about the simple steps and formula used for the conversion, then you can refer to octal to binary formula to get every required things.

There are two approaches that can be used to create a program for octal to binary conversion in C++:

- Octal to Binary Direct Conversion
- Octal to Binary Indirect Conversion

In indirect conversion, convert given octal number into its equivalent decimal value first. And then convert the decimal to its equivalent binary value. But here, the program is created on direct conversion only. Because you can approach indirect conversion with yourself. The program for indirect approach is already given in separate article.

To convert octal number to binary number in C++ programming, you have to ask from user to enter the octal number and then convert it into its equivalent binary number. And finally display the binary value on output as shown in the program given below:

The question is, **write a program in C++ that converts octal to binary.** The answer to this question is given
below:

#include<iostream> #include<string.h> using namespace std; int main() { int octalNum, rev=0, rem, chk=0; char binaryNum[40] = ""; cout<<"Enter the Octal Number: "; cin>>octalNum; while(octalNum!=0) { rem = octalNum%10; if(rem>7) { chk++; break; } rev = (rev*10) + rem; octalNum = octalNum/10; } if(chk==0) { octalNum = rev; cout<<"\nEquivalent Binary Value: "; while(octalNum!=0) { rem = octalNum%10; switch(rem) { case 0: strcat(binaryNum, "000"); break; case 1: strcat(binaryNum, "001"); break; case 2: strcat(binaryNum, "010"); break; case 3: strcat(binaryNum, "011"); break; case 4: strcat(binaryNum, "100"); break; case 5: strcat(binaryNum, "101"); break; case 6: strcat(binaryNum, "110"); break; case 7: strcat(binaryNum, "111"); break; } octalNum = octalNum/10; } cout<<binaryNum; } else cout<<"\nInvalid Octal Digit!"; cout<<endl; return 0; }

This program was build and run under *Code::Blocks* IDE. Here is its sample run:

Now supply any octal number say **364** as input and then press `ENTER`

key convert and print
its binary equivalent as shown in the output given below:

The function **strcat()** is used to concatenate string.
It takes two string as its argument. The value of second string gets appended at the end of first string.

The following block of code (included in above program):

while(octalNum!=0) { rem = octalNum%10; if(rem>7) { chk++; break; } rev = (rev*10) + rem; octalNum = octalNum/10; }

is used to check whether any digit of given octal number is greater than 7 or not. If any digit found as greater than 7, then that digit is not a valid octal digit. Because octal number can only be of 8 digits, that are from 0 to 7.

The **while loop** executes in following ways (supposing user input as **364**):

- The condition
**octalNum!=0**or**364!=0**evaluates to be true, therefore program flow goes inside the loop - There,
**octalNum%10**or**364%10**or**4**gets initialized to**rem** - And the condition of
**if**, i.e.,**rem>7**evaluates to be false, therefore program flow does not goes inside**if**'s body, rather it goes to the statement that is available after the**if**'s block of code - That is,
**(rev*10)+rem**or**(0*10)+4**or**4**gets initialized to**rev** - And the final statement of
**while loop**, gets evaluated, that is**octalNum/10**or**364/10**or**36**gets initialized to**octalNum** - Program flow goes back and evaluates the condition of
**while loop**again. - The evaluation of
**while loop**continues until the value of**octalNum**becomes equal to 0. That is the condition evaluates to be false - Because, no any digit in
**364**is greater than 7, therefore program flow never goes inside the**if**'s body - That means, the value of
**chk**is 0 (initialized at start of the program) - Therefore after the ending of
**while loop**, we've checked whether the value of**chk**holds its initial value (0) or not - If it holds its initial value, then process further to convert octal number to binary. Otherwise, print a
message like
**An Invalid Octal Digit was Entered!** - Now let's perform the dry run of all block of codes inside the
**if(chk==0)**block, given below

So the dry run of above program with user input **364** goes like (Inside the **if** block):

**binaryNum=""**,**rev=463**(as reversed the number earlier using**while loop**- The value of
**rev**(463) gets initialized to**octalNum**. So**octalNum=463** - Now the condition of
**while loop**(inside**if**) gets evaluated - That is, the condition,
**octalNum!=0**or**463!=0**evaluates to be true, therefore program flow goes inside the loop and**octalNum%10**or**463%10**or**3**gets initialized to**rem** - Now using
**switch**, the value of**rem**gets compared with all of the 8 cases. That is, its value is equal to 0, 1, 2, ...., or 7 - Whatever its value is, concatenates its binary equivalent to
**binaryNum**variable - Because the value of
**rem**is 3, therefore**case 3:**'s statement gets evaluated. That is,

`strcat(binaryNum, "011");`

gets evaluated. Therefore,**binaryNum=011**(since initial value of**binaryNum**is empty). From next time, binary equivalent gets added after**011** - And
**octalNum/10**or**463/10**or**46**gets initialized to**octalNum**. So the new value of**octalNum**is**46** - Now program flow goes back and evaluates the condition of
**while loop**again with new value of**octalNum** - That is, the condition,
**octalNum!=0**or**46!=0**evaluates to be true and program flow again goes inside the loop - The process continues, until the condition of
**while loop**evaluates to be false - On continuing the process of evaluating
*while loop*'s block of code, we'll get values as:- rem=3, binaryNum=011, octalNum=46
- rem=6, binaryNum=011110, octalNum=4
- rem=4, binaryNum=011110100, octalNum=0

- Now print the value of
**binaryNum**on output, that will be the binary equivalent (011110100) of given octal number (364)

Now let's create another program that only prints binary equivalent of given octal number by user at run-time without actually converting it.

#include<iostream> using namespace std; int main() { int octalNum, rev=0, rem; cout<<"Enter the Octal Number: "; cin>>octalNum; while(octalNum!=0) { rem = octalNum%10; rev = (rev*10) + rem; octalNum = octalNum/10; } octalNum = rev; cout<<"\nEquivalent Binary Value: "; while(octalNum!=0) { rem = octalNum%10; switch(rem) { case 0: cout<<"000"; break; case 1: cout<<"001"; break; case 2: cout<<"010"; break; case 3: cout<<"011"; break; case 4: cout<<"100"; break; case 5: cout<<"101"; break; case 6: cout<<"110"; break; case 7: cout<<"111"; break; default: cout<<"--InvalidOctalDigit("<<rem<<")--"; break; } octalNum = octalNum/10; } cout<<endl; return 0; }

Here is its sample run with user input **254** (octal number):

If user enters an octal number that contains one or more invalid octal digit say **2948**. Then here is the
output you'll see:

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.