# JAVA Program to Convert Binary to Octal

Binary Number − There are four types of number systems available. Binary numbers are one of them. The Binary number is basically represented with two digits i.e. one (1) and zero (0). The binary numbers are expressed as base-2 in the numeral system.

Octal Number − Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7(0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.

Here we are converting the Binary number to octal number but for this we have to first convert the binary number into decimal number so that further we can convert that decimal number into octal number.

Binary Number → Decimal Number → Octal Number


In this article we will see how to convert binary to octal in Java.

## To show you some instances −

### Instance-1

Input Binary number is 101011.
By converting into decimal = 43.
Now converting it to octal = 53


### Instance-2

Input Binary number is 1111111.
By converting into decimal = 127
Now converting it to octal = 177


### Instance-3

Input Binary number is 11001100.
By converting into decimal = 204.
Now converting it to octal = 314.


### Algorithm

Step-1 − Get the input number either by static input or user input method.

Step-2 − We declared two user defined methods. One method is used to convert the Binary number into a Decimal number. In another method we are converting the decimal number into octal.

Step-3 − When we convert the binary into decimal, we multiply the power of 2 in increasing order so that we can get the appropriate decimal value.

Step-4 − In another user defined method we are converting the decimal value into octal value.

Step-5 − By using this method we are converting the Binary number into octal number.

### Multiple Approaches

We have provided the solution in different approaches.

• By User Defined Method with Static Input Values.

• By User Defined Method with User Input Values.

Let’s see the program along with its output one by one.

## Approach-1: By Using User Defined Method with Static Input Value

In this approach, we declare a long variable and initialize it with a binary number in the program and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can convert the binary number into octal number.

## Example

import java.util.*;
public class Main {
public static void main(String[] args){
long binary=1010;
binToDec(binary);
}
public static void binToDec(long binaryNumber){
int decNum = 0, i = 0;
while (binaryNumber > 0) {
decNum += Math.pow(2, i++) * (binaryNumber % 10);
binaryNumber /= 10;
}
System.out.println("In Decimal="+decNum);
decToOct(decNum);
}
public static void decToOct(long decNum){
String octalString = Long.toOctalString(decNum);
int octalNumber = Integer.parseInt(octalString);
System.out.println("In octal="+octalNumber);
}
}


## Output

In Decimal=10
In octal=12


## Approach-2: By Using User Defined Method with User Input Value

In this approach, we declare a binary input number and pass this number as parameter in a user defined method, then inside the method by using the algorithm we can convert the binary number into hexadecimal number.

## Example

import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary number: ");
long binary=sc.nextLong();
binToDec(binary);
}
public static void binToDec(long binaryNumber){
int decNum = 0, i = 0;
while (binaryNumber > 0) {
decNum += Math.pow(2, i++) * (binaryNumber % 10);
binaryNumber /= 10;
}
System.out.println("In Decimal="+decNum);
decToOct(decNum);
}
public static void decToOct(long decNum){
String octalString = Long.toOctalString(decNum);
int octalNumber = Integer.parseInt(octalString);
System.out.println("In octal="+octalNumber);
}
}


## Output

Enter a binary number: 1111
In Decimal=15
In octal=17


In this article, we explored how to convert binary to octal in Java by using different approaches.

Advertisements