# Division of two binary numbers in Java

Today, we will learn the division of two binary numbers in java. This article will help us to increase our knowledge about some built-in methods.

There are multiple ways to do so :

1. We can first convert them to an integer where we do the required task and then again convert them back to binary numbers.
2. In this method, we can use some direct algorithm that is given in many books.

So, now the question is which method we should use? In the second method often some difficulties may arise like time complexity, digits precision and so on. Therefore, we will go with the first method as it will be fast and easy to understand.

Subtraction of two binary numbers in Java

## Division of two binary numbers: Java Code

```import java.util.*;
public class Main{

public static void main(String []args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter dividend: ");

/*Iteger.parseInt() method will convert the string value to
the integer by treating them as a binary number*/

int dividend = Integer.parseInt(scan.nextLine(),2);
System.out.println("\nEnter divisor: ");
int divisor = Integer.parseInt(scan.nextLine(),2);
if(divisor!=0)
{
String quotient = Integer.toBinaryString((dividend/divisor));
String remainder = Integer.toBinaryString((dividend%divisor));
System.out.println("\nquotient is: "+quotient+" and remainder is: "+remainder);
}
else
System.out.println("\ndivisor can't be zero!");
}
}```

Output:

```Enter dividend:
01000             //input due to line 7
Enter divisor:
100              //input due to line 9
quotient is: 10 and remainder is: 0

```

Final answers i.e. quotient and remainder are also in binary numbers. But, what if we give zero as input to divisor in such case our if() condition will make sure that no division by zero condition may arise. In such a case, the output will look like this:

```Enter dividend:
01000
Enter divisor:
0                 //input zero to divisor which can't be possible
divisor can't be zero!

```

Now you know how to divide two binary numbers in java in a pretty easy manner.