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

Program to convert a number from binary to octal using C, C++, Java and other programming languages is discussed here. Firstly let us look at an example to do this.

For example,

  • Consider the binary number 1111 which has to be converted to octal.
  • Divide the binary number into groups of three from right to left.

1111 can be grouped as 001  111

  • Now, find the octal number for each group.

Three-bit binary numbers from (000 to 111) have equal decimal and octal representations. Hence, the octal numbers for binary numbers in the range (000 to 111) are same as that of the decimal numbers.

The octal number of 001 is 1 and 111 is 7.

The octal equivalent of 1111 is 17.

Algorithm to convert a number from binary to octal

  • Input the binary number.
  • Divide the binary number into groups of three from right to left.
  • Find the decimal equivalent of each group.
  • The decimal equivalent of each group from left to right gives the equivalent octal number of the given binary number.

Program to convert a number from binary to octal

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

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

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

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

i = 1;

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

return octal;
}

int main()
{
long int binary;

printf(“\nEnter a binary number: “);
scanf(“%lld”, &binary);

printf(“\nOctal Equivalent : %d\n”, binary_to_octal(binary));

return 0;
}

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

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

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

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

i = 1;

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

return octal;
}

int main()
{
long int binary;

cout << “\nEnter a binary number: “;
cin >> binary;

cout << “\nOctal Equivalent : ” << binary_to_octal(binary) << endl;

return 0;
}

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

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

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

i = 1;

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

return octal;
}

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

# Python program to convert a number from binary to octal

import math
def binary_to_octal(binary):
octal = 0
decimal = 0
i = 0

while(binary != 0):
decimal += (binary%10) * math.pow(2,i);
i = i + 1
binary = int(binary/10)

i = 1

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

return octal;

return decimal;

binary = int(input(“Enter the binary number : “))
print(“Equivalent decimal number : “,int(binary_to_octal(binary)))

Output:

binary to octal

Time complexity: O(n)