Binary to octal conversion using Java

Binary to Octal Conversion

In this article we will discuss binary to octal conversion using java. For this purpose we need to ask a binary number from user and convert that binary number to its octal equivalent form and then print the converted number on to the screen.
 
For conversion, we first convert the binary number into decimal form and then convert the decimal number to octal number. For better understanding with an example just have a look towards the diagram shown at the right.
Binary to octal conversion

Method discussed

We will first convert the Binary number into Decimal and then convert the Decimal number into Octal

Make sure that you have gone through these approaches below –

  1. Binary to Decimal Conversion
  2. Decimal to Octal Conversion
Run
//Java program to convert binary number to octal number
import java.util.Scanner;
public class Main
{
	public static void main(String args[])
	{
		//scanner class object creation
		Scanner sc = new Scanner(System.in);    
		//input from user
		System.out.print("Enter a binary number : ");
		int binary = sc.nextInt();
		//Declaring variable to store decimal number  
		int decimal = 0;
		//Declaring variable to use in power		
		int n = 0;  
		//writing logic for the conversion from binary to decimal
		while(binary > 0)
		{
			int temp = binary%10;  
			decimal += temp*Math.pow(2, n);  
			binary = binary/10;  
			n++;  
		}
		int octal[] = new int[20];
		int i = 0;
		//writing logic for the conversion from decimal to octal
		while(decimal > 0)
		{
			int r = decimal % 8;
			octal[i++] = r;
			decimal = decimal / 8;
		}
		//printing result
		System.out.print("Octal number : ");
		for(int j = i-1 ; j >= 0 ; j--)
		System.out.print(octal[j]); 
		//closing scanner class(not compulsory, but good practice)
		sc.close();
	}
}

Method 1 Code

Output :

Enter binary number: 1010
Decimal : 10
Octal : 12

Method 2:-

Method 2 uses the concept of grouping 3 successive digits/bits of the binary number and calculating octal digits against each grouping

We use an additional array to store the octal digits at each index.

We will need to print the array in reverse to get actual octal equivalent.

Binary to octal conversion

Method 2 Code

Run
//Java program to convert binary number to octal number
import java.util.Scanner;
public class Main
{
	public static void main(String args[])
	{
		//scanner class object creation
		Scanner sc = new Scanner(System.in);    
		//input from user
		System.out.print("Enter a binary number : ");
		int binary = sc.nextInt();
		//Declaring variable to store decimal number  
		int decimal = 0;
		//Declaring variable to use in power		
		int n = 0;  
		//writing logic for the conversion from binary to decimal
		while(binary > 0)
		{
			int temp = binary%10;  
			decimal += temp*Math.pow(2, n);  
			binary = binary/10;  
			n++;  
		}
		int octal[] = new int[20];
		int i = 0;
		//writing logic for the conversion from decimal to octal
		while(decimal > 0)
		{
			int r = decimal % 8;
			octal[i++] = r;
			decimal = decimal / 8;
		}
		//printing result
		System.out.print("Octal number : ");
		for(int j = i-1 ; j >= 0 ; j--)
		System.out.print(octal[j]); 
		//closing scanner class(not compulsory, but good practice)
		sc.close();
	}
}

Output :

Enter binary number: 10101111
257

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

7 comments on “Binary to octal conversion using Java”


  • Chetan

    String num = sc.nextLine();
    try{
    int decimal= Integer.parseInt(num,2); // binary to decimal conversion.
    String octal=Integer.toOctalString(decimal);
    System.out.println(octal);}
    catch (Exception e){
    System.out.println(“Invalid binary input. Please enter a valid binary number.”);
    }


  • Tridip

    import java.util.*;
    public class Binarytooctal {

    public static long binarytodecimal(int binary){

    long decimal =0;
    int x=0;

    while(binary >0){

    long rem = binary %10;

    decimal += rem * Math.pow(2,x);

    binary /=10;

    }

    return decimal;

    }

    public static List decimaltooctal(long decimal){

    List a = new ArrayList();
    long octal =0;

    while(decimal >0){

    long rem = decimal % 8;

    a.add(rem);

    decimal /=10;

    }
    return a;

    }

    public static void main(String[] args){

    System.out.print(decimaltooctal(binarytodecimal(111)));

    }

    }


  • pjanhabi584

    import java.util.*;
    public class Binary_Octal
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter a Binary number : “);
    int num=Integer.parseInt(sc.nextLine(),2);
    String octal=Integer.toOctalString(num);
    System.out.println(“Octal version is : “+octal);
    }
    }


  • Kundan

    import java.util.*;
    class main{
    static String octal(String binary){
    int temp = 0;
    StringBuilder octal = new StringBuilder();
    int j = 0;
    for(int i=binary.length()-1;i>=0;i–){
    if(j == 3){
    octal.append(temp);
    System.out.println(j+” “+temp);
    temp = 0;
    j=0;
    }
    temp += (binary.charAt(i)-‘0’)*Math.pow(2,j);
    j++;
    }
    octal.append(temp);
    return “Binary : “+binary+” == octal : “+octal.reverse().toString();
    }
    public static void main(String[] args) {
    String binary = “1011001”;
    System.out.println(octal(binary));
    }
    }


  • anurag

    package com.company;
    import java.util.*;
    class Main {
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println(“enter the binary number”);
    int binary=input.nextInt();
    int l=0,b=0;
    int decimal=0;
    int octal=0;
    while(binary!=0)
    {
    b=binary%10;
    decimal=decimal+(b*((int)Math.pow(2,l)));
    binary=binary/10;
    b=0;
    l++;
    }l=0;
    System.out.println(“decimal”+decimal);
    while(decimal!=0)
    {
    b=decimal%8;
    // System.out.println(b);
    octal=octal+(b*(int)Math.pow(10,l));
    // System.out.println(octal);
    decimal=decimal/8;
    l++;
    // System.out.println(decimal);
    }
    System.out.println(“octal”+octal);
    }
    }