Octal to Binary Conversion

Octal to Binary Conversion” is one of the classic programming problem exercise for beginners. Here, we are given an octal number, entered by user and our task is to write a program to convert the given octal number to its equivalent binary number. 

Example:

Octal = 65
Binary = 110101
Octal = 78
Binary = 11110000

The steps to convert octal number to binary numbers are as follows:

  1. Scan the octal number from left to right.
  2. Convert each digit of octal number into corresponding 3-digit binary number.
  3. Combine all the 3-digit binary number to get the result.
  4. Truncate the front 0s of the binary number, if exist.
  5. Print the result.

C++ Program to convert octal number to binary number is as follows:

/* C++ Program to Convert Octal Number to Binary Number */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    /* Scan the Octal Number */  
    int Octal;  
    cout<<"Enter the Octal Number: ";  
    cin>>Octal;  
      
    /* Converting Octal to Binary*/  
    string Binary;  
    int temp = Octal;  
    while(temp != 0)  
    {  
        int remainder = temp % 10;  
        string val;  
          
        /* Binary equivalent of each digit */  
        switch(remainder) {  
            case 0: val = "000";  
            break;  
            case 1: val = "001";  
            break;  
            case 2: val = "010";  
            break;  
            case 3: val = "011";  
            break;  
            case 4: val = "100";  
            break;  
            case 5: val = "101";  
            break;  
            case 6: val = "110";  
            break;  
            case 7: val = "111";  
            break;  
            case 8: val = "1000";  
            break;  
            case 9: val = "1001";  
            break;  
        }  
          
        Binary = val + Binary ;  
        temp = temp / 10;  
    }  
      
    /*Deleting the extra zeros from front*/  
    while(Binary[0]!='1')  
    {  
        Binary.erase(Binary.begin()+0);  
    }  
      
    /*Printing the result */  
    cout<<"The Binary of "<<Octal<<" is "<<Binary;  
}  
OUTPUT: 
Enter the Octal Number: 78
The Binary of 78 is 1111000

Related Posts:

  1. Program to convert octal number to decimal number.
  2. Program to convert octal number to hexadecimal number.
  3. Program to convert decimal number to binary number.
  4. Program to convert decimal number to octal number.
  5. Program to convert decimal number to hexadecimal number.
  6. Program to convert hexadecimal number to binary number.
  7. Program to convert hexadecimal number to octal number.
  8. Program to convert hexadecimal number to decimal number.
  9. Program to convert binary number to octal number.
  10. Program to convert binary number to decimal number.
  11. Program to convert binary number to hexadecimal number.
  12. Program to check Leap Year.
  13. Program to find sum of first ‘n’ natural numbers.
  14. Program to Reverse a Number.
  15. Program to swap two Numbers.
  16. Program to print Fibonacci Series up to Nth Term.
  17. Program to find and print nth Fibonacci number.
  18. Program to find Power of the Number.
  19. Program to find Quotient and Remainder.
  20. Program to find largest amongst three numbers.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *