Program to Convert Octal Number to Binary Number
Last Updated :
10 Apr, 2023
Given an Octal number as input, the task is to convert that number to Binary number.
Examples:
Input : Octal = 345
Output : Binary = 11100101
Explanation :
Equivalent binary value of 3: 011
Equivalent binary value of 4: 100
Equivalent binary value of 5: 101
Input : Octal = 120
Output : Binary = 1010000
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++
#include <iostream>
using namespace std;
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;
}
int main()
{
string octnum = "345" ;
cout << "Equivalent Binary Value = "
<< OctToBin(octnum);
return 0;
}
|
Java
import java.util.*;
class Solution
{
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;
}
public static void main(String args[])
{
String octnum = "345" ;
System.out.println( "Equivalent Binary Value = " + OctToBin(octnum));
}
}
|
Python3
def OctToBin(octnum):
binary = ""
while octnum ! = 0 :
d = int (octnum % 10 )
if d = = 0 :
binary = " ".join([" 000 ", binary])
elif d = = 1 :
binary = " ".join([" 001 ", binary])
elif d = = 2 :
binary = " ".join([" 010 ", binary])
elif d = = 3 :
binary = " ".join([" 011 ", binary])
elif d = = 4 :
binary = " ".join([" 100 ", binary])
elif d = = 5 :
binary = " ".join([" 101 ", binary])
elif d = = 6 :
binary = " ".join([" 110 ",binary])
elif d = = 7 :
binary = " ".join([" 111 ", binary])
else :
binary = "Invalid Octal Digit"
break
octnum = int (octnum / 10 )
return binary
octnum = 345
final_bin = "" + OctToBin(octnum)
print ( "Equivalent Binary Value =" , final_bin)
|
C#
class GFG
{
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;
}
static void Main()
{
string octnum = "345" ;
System.Console.WriteLine( "Equivalent Binary Value = " +
OctToBin(octnum));
}
}
|
PHP
<?php
function OctToBin( $octnum )
{
$i = 0;
$binary = (string) "" ;
while ( $i != strlen ( $octnum ))
{
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 :
echo ( "\nInvalid Octal Digit " .
$octnum [ $i ]);
break ;
}
$i ++;
}
return $binary ;
}
$octnum = "345" ;
echo ( "Equivalent Binary Value = " .
OctToBin( $octnum ));
|
Javascript
<script>
function OctToBin(octnum)
{
let i = 0;
let binary = "" ;
while (i<octnum.length) {
let 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 :
document.write( "<br>Invalid Octal Digit " + octnum[i]);
break ;
}
i++;
}
return binary;
}
let octnum = "345" ;
document.write( "Equivalent Binary Value = " + OctToBin(octnum));
</script>
|
Output:
Equivalent Binary Value = 011100101
Time complexity: O(n) where n is no of digits in a given number.
Auxiliary space: O(n) it is using extra space for binary string.
define function to convert octal to binary in python:
Approach:
n this program, we define a function called octal_to_binary that takes an octal number as input and returns its binary equivalent.
The function works by first converting the octal number to decimal using the repeated division by 8 technique. Once we have the decimal equivalent, we can convert it to binary using the repeated division by 2 technique.
Finally, we call the octal_to_binary function with a sample octal number of 777, and then print the binary equivalent using an f-string.
C++
#include <iostream>
#include <cmath>
using namespace std;
int octal_to_binary( int octal) {
int decimal = 0;
int power = 0;
while (octal != 0) {
decimal += (octal % 10) * pow (8, power);
octal /= 10;
power++;
}
int binary = 0;
int digit_place = 1;
while (decimal != 0) {
binary += (decimal % 2) * digit_place;
decimal /= 2;
digit_place *= 10;
}
return binary;
}
int main() {
int octal_number = 777;
int binary_number = octal_to_binary(octal_number);
cout << binary_number << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
public static int octal_to_binary( int octal)
{
int decimal = 0 ;
int power = 0 ;
while (octal != 0 ) {
decimal += (octal % 10 ) * Math.pow( 8 , power);
octal /= 10 ;
power++;
}
int binary = 0 ;
int digit_place = 1 ;
while (decimal != 0 ) {
binary += (decimal % 2 ) * digit_place;
decimal /= 2 ;
digit_place *= 10 ;
}
return binary;
}
public static void main(String[] args)
{
int octal_number = 777 ;
int binary_number = octal_to_binary(octal_number);
System.out.println(binary_number);
}
}
|
Python3
def octal_to_binary(octal):
decimal = 0
power = 0
while octal ! = 0 :
decimal + = (octal % 10 ) * pow ( 8 , power)
octal / / = 10
power + = 1
binary = 0
digit_place = 1
while decimal ! = 0 :
binary + = (decimal % 2 ) * digit_place
decimal / / = 2
digit_place * = 10
return binary
octal_number = 777
binary_number = octal_to_binary(octal_number)
print (f "The binary equivalent of {octal_number} is {binary_number}" )
|
C#
using System;
class Program {
static int octal_to_binary( int octal) {
int decimalNumber = 0;
int power = 0;
while (octal != 0) {
decimalNumber += (octal % 10) * ( int )Math.Pow(8, power);
octal /= 10;
power++;
}
int binaryNumber = 0;
int digit_place = 1;
while (decimalNumber != 0) {
binaryNumber += (decimalNumber % 2) * digit_place;
decimalNumber /= 2;
digit_place *= 10;
}
return binaryNumber;
}
static void Main() {
int octalNumber = 777;
int binaryNumber = octal_to_binary(octalNumber);
Console.WriteLine(binaryNumber);
}
}
|
Javascript
function octal_to_binary(octal)
{
let decimal = 0;
let power = 0;
while (octal != 0) {
decimal += (octal % 10) * Math.pow(8, power);
octal = Math.floor(octal / 10);
power++;
}
let binary = 0;
let digit_place = 1;
while (decimal != 0) {
binary += (decimal % 2) * digit_place;
decimal = Math.floor(decimal / 2);
digit_place *= 10;
}
return binary;
}
const octal_number = 777;
const binary_number = octal_to_binary(octal_number);
console.log(`The binary equivalent of ${octal_number} is ${binary_number}`);
|
Output
The binary equivalent of 777 is 111111111
The time complexity: O(log n). Where n is the input octal number
The auxiliary space : O(1) .
Similar Reads
Program to convert a binary number to octal
The problem is to convert the given binary number (represented as string) to its equivalent octal number. The input could be very large and may not fit even into unsigned long long int. Examples: Input : 110001110 Output : 616 Input : 1111001010010100001.010110110011011 Output : 1712241.26633 The id
9 min read
Program to convert a binary number to hexadecimal number
Given a Binary Number, the task is to convert the given binary number to its equivalent hexadecimal number. The input could be very large and may not fit even into an unsigned long long int. Examples:Â Input: 110001110Output: 18EInput: 1111001010010100001.010110110011011Output: 794A1.5B36 794A1D9B A
13 min read
Program to convert a Binary Number to Hexa-Decimal Number
Given a Binary Number, the task is to convert this Binary number to its equivalent Hexa-Decimal Number.Examples: Input: 100000101111 Output: 82F Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1111. Here, 1000 is equivalent to 8, 0010 is equivalent to 2 and 1111 is equivalent
10 min read
Program to Convert Hexadecimal Number to Binary
Given a Hexadecimal number as an input, the task is to convert that number to a Binary number.Examples: Input: Hexadecimal = 1AC5 Output: Binary = 0001101011000101 Explanation: Equivalent binary value of 1: 0001 Equivalent binary value of A: 1010 Equivalent binary value of C: 1100 Equivalent binary
14 min read
Program to convert float decimal to Octal number
Write a program that converts a floating-point decimal number into its octal format. The program should take a number and the precision for the octal fractional part as inputs from the user. It should calculate and provide its correct octal representation as output. Examples: Input: Number = 123.45,
8 min read
Number of steps required to convert a binary number to one
Given a binary string str, the task is to print the numbers of steps required to convert it to one by the following operations: If 'S' is having odd value, add 1 to it.If 'S' is having even value, divide it by 2.Examples: Input: str = "1001001" Output: 12 Input: str = "101110" Output: 8Number '10111
7 min read
Convert all numbers in range [L, R] to binary number
Given two positive integer numbers L and R. The task is to convert all the numbers from L to R to binary number. Examples: Input: L = 1, R = 4Output: 11011100Explanation: The binary representation of the numbers 1, 2, 3 and 4 are: 1 = (1)22 = (10)23 = (11)24 = (100)2 Input: L = 2, R = 8Output:101110
5 min read
Program to Convert BCD number into Decimal number
Given a BCD (Binary Coded Decimal) number, the task is to convert the BCD number into its equivalent Decimal number. Examples: Input: BCD = 100000101000 Output: 828 Explanation: Dividing the number into chunks of 4, it becomes 1000 0010 1000. Here, 1000 is equivalent to 8 and 0010 is equivalent to 2
10 min read
Program for Binary To Decimal Conversion
Given a binary number as input, we need to write a program to convert the given binary number into an equivalent decimal number. Examples : Input : 111Output : 7Explanation : The output of 7 for input 111 represents the decimal equivalent of the binary number 111. Input : 1010Output : 10Explanation
15+ min read
Program for Decimal to Binary Conversion
Given a decimal number n, the task is to convert the given decimal number into an equivalent binary number. Examples:Â Input: n = 12Output: "1100"Explanation: Input: n = 17Output: "10001"Explanation: Table of Content [Approach - 1] Division by 2 - O(log n) Time and O(1) Space[Approach - 2] Using Hea
11 min read