# Convert a number from octal to binary in C, C++, Java and Python | faceprep

Program to convert a number from octal to binary is discussed here. Firstly, the given octal number is converted to a decimal number. Then, the decimal number is converted to binary.

For example, consider the octal number 17 which has to be converted to binary.

The decimal equivalent of the octal  number 17 is calculated as follows

• 7 * 8^0 = 7
• 1 * 8^1 = 8

Decimal equivalent : 15 (8 + 7)

Now, this 15 is converted to binary

• 15 / 2 = 7 , rem = 1
• 7 / 2 = 3 , rem = 1
• 3 / 2 = 1 , rem = 1
• 1 / 2 = 0 , rem = 1

Binary Equivalent : 1111

## Algorithm to convert a number from octal to binary

Firstly, convert the number from octal to decimal.

• Input the octal number.
• Count the total number of digits in the given number.
• Assume that the number has n digits.
•  Multiply each digit in the number with 8^(n-1), when the digit is in the nth position.
•  Perform addition of all the digits after multiplication.
• The added sum gives the decimal equivalent of the octal number.

Then, convert the decimal number to binary,

• Divide the number by 2.
• Note the remainder during each division.
• Continue to divide until the number becomes 0.
• The Binary equivalent will be the reverse of the remainder.

## Program to convert a number from octal to binary

// C program to convert a number from octal to binary

#include <stdio.h>
#include <math.h>

long long octal_to_binary(int octal)
{
int decimal = 0, i = 0;
long long binary = 0;

while(octal != 0)
{
decimal += (octal%10) * pow(8,i);
++i;
octal/=10;
}

i = 1;

while (decimal != 0)
{
binary += (decimal % 2) * i;
decimal /= 2;
i *= 10;
}

return binary;
}

int main()
{
int octal;

printf(“\nEnter an octal number: “);
scanf(“%d”, &octal);

printf(“\nBinary Equivalent : %d\n”, octal_to_binary(octal));

return 0;
}

// C++ program to convert a number from octal to binary

#include <iostream>
#include <math.h>
using namespace std;

long long octal_to_binary(int octal)
{
int decimal = 0, i = 0;
long long binary = 0;

while(octal != 0)
{
decimal += (octal%10) * pow(8,i);
++i;
octal/=10;
}

i = 1;

while (decimal != 0)
{
binary += (decimal % 2) * i;
decimal /= 2;
i *= 10;
}

return binary;
}

int main()
{
int octal;

cout << “\nEnter an octal number: “;
cin >> octal;
cout << “\nBinary Equivalent : ” << octal_to_binary(octal);

return 0;
}

// Java program to convert a number from octal to binary

import java.util.*;
public class Main
{
public static int octal_to_binary(int octal)
{
int decimal = 0, i = 0;
int binary = 0;

while(octal != 0)
{
decimal += (octal%10) * Math.pow(8,i);
++i;
octal/=10;
}

i = 1;

while (decimal != 0)
{
binary += (decimal % 2) * i;
decimal /= 2;
i *= 10;
}

return binary;
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter the octal number : “);
int octal = sc.nextInt();
System.out.print(“\nEquivalent octal number : ” + octal_to_binary(octal));
}
}

# Python program to convert a number from octal to binary

import math
def octal_to_binary(octal):
decimal = 0
i = 0
binary = 0
while(octal != 0):
decimal += (octal%10) * math.pow(8,i)
i = i + 1
octal = int (octal / 10)

i = 1

while (decimal != 0):
binary = binary + (decimal % 2) * i;
decimal = int(decimal / 2);
i *= 10;

return binary

octal = int(input(“Enter an octal number : “))
print(“Binary Equivalent : “,int(octal_to_binary(octal)))

Output:

Time complexity: O(n)