Program to Convert Octal Number to Binary Number

Given an Octal number as input, the task is to convert that number to Binary number.

Examples:

Input : Octal = 345
Output : Binary = 011100101
Explanation : 
Equivalent binary value of 3: 011
Equivalent binary value of 4: 100
Equivalent binary value of 5: 101

Input : Octal = 120
Output : Binary = 001010000


Octal Number: An Octal number is a positional numeral system with a radix, or base, of 8 and uses eight distinct symbols.

Binary Number: A Binary number is a number expressed in the base-2 binary numeral system, which uses only two symbols: which are 0 (zero) and 1 (one).

To convert an Octal number to Binary, the binary equivalent of each digit of the octal number is evaluated and combined at the end to get the equivalent binary number.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to convert
// Octal number to Binary
  
#include <iostream>
using namespace std;
  
// Function to convert an
// Octal to Binary Number
string OctToBin(string octnum)
{
    long int i = 0;
  
    string binary = "";
  
    while (octnum[i]) {
        switch (octnum[i]) {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            cout << "\nInvalid Octal Digit "
                 << octnum[i];
            break;
        }
        i++;
    }
  
    return binary;
}
  
// Driver code
int main()
{
    // Get the Hexadecimal number
    string octnum = "345";
  
    // Convert Ocatl to Binary
    cout << "Equivalent Binary Value = "
         << OctToBin(octnum);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to convert
// Octal number to Binary
import java.util.*;
class Solution
{
   
// Function to convert an
// Octal to Binary Number
static String OctToBin(String octnum)
{
    long i = 0;
   
    String binary = "";
      
    while (i<octnum.length()) {
          
        char c=octnum.charAt((int)i);
        switch (c) {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            System.out.println( "\nInvalid Octal Digit "+ octnum.charAt((int)i));
            break;
        }
        i++;
    }
   
    return binary;
}
   
// Driver code
public static void main(String args[])
{
    // Get the Hexadecimal number
    String octnum = "345";
   
    // Convert Ocatl to Binary
    System.out.println( "Equivalent Binary Value = "+ OctToBin(octnum));
   
}
  
}
//contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert Octal number to Binary
  
class GFG
{
  
// Function to convert an
// Octal to Binary Number
static string OctToBin(string octnum)
{
    int i = 0;
  
    string binary = "";
      
    while (i < octnum.Length) 
    {
          
        char c = octnum[i];
        switch (c) 
        {
        case '0':
            binary += "000";
            break;
        case '1':
            binary += "001";
            break;
        case '2':
            binary += "010";
            break;
        case '3':
            binary += "011";
            break;
        case '4':
            binary += "100";
            break;
        case '5':
            binary += "101";
            break;
        case '6':
            binary += "110";
            break;
        case '7':
            binary += "111";
            break;
        default:
            System.Console.WriteLine( "\nInvalid Octal Digit "
                                                    octnum[i]);
            break;
        }
        i++;
    }
  
    return binary;
}
  
// Driver code
static void Main()
{
    // Get the Hexadecimal number
    string octnum = "345";
  
    // Convert Ocatl to Binary
    System.Console.WriteLine("Equivalent Binary Value = "
                                         OctToBin(octnum));
}
}
  
// This code is contributed by mits

chevron_right


PHP

Output:

Equivalent Binary Value = 011100101


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.