Java Program to divide two binary numbers using ParseInt
Program
import java.util.Scanner;
public class DivideTwoBinaryNumbersUsingParseInt {
public void divideBinaryNumbers(String binaryNum1, String binaryNum2)
{
int num1 = Integer.parseInt(binaryNum1, 2);
int num2 = Integer.parseInt(binaryNum2, 2);
try{
int quotient = num1 / num2;
System.out.println(Integer.toBinaryString(quotient));
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero exception - Zero cannot divide any number");
}
}
public static void main(String[] args)
{
DivideTwoBinaryNumbersUsingParseInt divideTwoBinaryNumbersUsingParseInt= new DivideTwoBinaryNumbersUsingParseInt();
Scanner reader = new Scanner(System.in);
System.out.print("Enter the first binary number: ");
String binaryNum1 = reader.nextLine();
System.out.print("Enter the second binary number: ");
String binaryNum2 = reader.nextLine();
System.out.print("Division of two numbers - Quotient is: ");
divideTwoBinaryNumbersUsingParseInt.divideBinaryNumbers(binaryNum1, binaryNum2);
}
}
This Java program performs division of two binary numbers by converting them to decimal, performing division, and then converting the quotient back to binary.
Use Case:
This program is useful when performing binary arithmetic operations like division, especially in cases where binary data representation is critical (e.g., low-level computations, computer architecture).
-
Input:
- The program uses the
Scanner
class to take two binary numbers as input from the user in the form of strings.
- The program uses the
-
Conversion to Decimal:
Integer.parseInt(binaryNum, 2)
:- Converts the binary input strings to their equivalent decimal integers.
- The
2
indicates that the input string is in binary format.
-
Division:
- The decimal numbers are divided using the
/
operator to compute the quotient.
- The decimal numbers are divided using the
-
Exception Handling:
- The program uses a
try-catch
block to handle division by zero:- If the second number (denominator) is zero, an
ArithmeticException
is thrown. - The catch block prints an error message: "Divide by zero exception - Zero cannot divide any number."
- If the second number (denominator) is zero, an
- The program uses a
-
Conversion Back to Binary:
Integer.toBinaryString(quotient)
:- Converts the quotient (a decimal number) back to its binary string representation.
-
Output:
- The program prints the binary representation of the quotient.
-
Example Execution:
-
Example 1:
Enter the first binary number: 1010 Enter the second binary number: 10 Division of two numbers - Quotient is: 101
- Convert
1010
(binary) to10
(decimal). - Convert
10
(binary) to2
(decimal). - Divide:
10 / 2 = 5
(decimal). - Convert
5
(decimal) to101
(binary).
- Convert
-
Example 2 (Division by Zero):
Enter the first binary number: 101 Enter the second binary number: 0 Division of two numbers - Quotient is: Divide by zero exception - Zero cannot divide any number
-
Key Features:
- Error Handling:
- Includes a mechanism to handle division by zero gracefully.
- Binary Arithmetic:
- Simplifies binary division using decimal arithmetic.
- Efficiency:
- Uses Java's built-in methods for binary-to-decimal conversion and vice versa, ensuring accuracy and simplicity.
Output 1
$ javac DivideTwoBinaryNumbersUsingParseInt.java
$ java DivideTwoBinaryNumbersUsingParseInt
Enter the first binary number: 10101
Enter the second binary number: 11
Division of two numbers - Quotient is: 111
Output 2
$ java DivideTwoBinaryNumbersUsingParseInt
Enter the first binary number: 11100
Enter the second binary number: 0
Division of two numbers - Quotient is: Divide by zero exception - Zero cannot divide any number