codescracker
c++

C++ Program to Convert Octal to Binary



« 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++:

  1. Octal to Binary Direct Conversion
  2. 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.

Octal to Binary in C++

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:

// C++ Program to Convert Octal to Binary
// ----codescracker.com----

#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:

C++ program convert octal to binary

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:

octal to binary c++

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):

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

Print Binary Equivalent of Octal Number

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

// Print Binary Equivalent of Given Octal Number
// ----codescracker.com----

#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):

octal to binary program c++

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

c++ program octal to binary

Same Program in Other Languages


« Previous Program Next Program »