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).

  1. Input:

    • The program uses the Scanner class to take two binary numbers as input from the user in the form of strings.
  2. 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.
  3. Division:

    • The decimal numbers are divided using the / operator to compute the quotient.
  4. 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."
  5. Conversion Back to Binary:

    • Integer.toBinaryString(quotient):
      • Converts the quotient (a decimal number) back to its binary string representation.
  6. Output:

    • The program prints the binary representation of the quotient.
  7. 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) to 10 (decimal).
      • Convert 10 (binary) to 2 (decimal).
      • Divide: 10 / 2 = 5 (decimal).
      • Convert 5 (decimal) to 101 (binary).
    • 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