Java

 

 

Introduction

A Binary Calculator is a tool designed to perform arithmetic operations such as addition, subtraction,
multiplication, and division using binary numbers. Binary arithmetic is the core of computer processing, as computers
utilize binary (base-2) numeral system to represent and perform calculations. The objective of this program is to provide
a simple yet efficient calculator that accepts binary inputs and performs standard binary operations.

Objective

The goal is to create a program in Java that:

  • Accepts two binary numbers as input from the user.
  • Performs arithmetic operations such as addition, subtraction, multiplication, and division on the binary numbers.
  • Displays the result of the operation in binary format.

Java Code for Binary Calculator

import java.util.Scanner;

public class BinaryCalculator {

    // Method to perform binary addition
    public static String addBinary(String a, String b) {
        int num1 = Integer.parseInt(a, 2);
        int num2 = Integer.parseInt(b, 2);
        int sum = num1 + num2;
        return Integer.toBinaryString(sum);
    }

    // Method to perform binary subtraction
    public static String subtractBinary(String a, String b) {
        int num1 = Integer.parseInt(a, 2);
        int num2 = Integer.parseInt(b, 2);
        int difference = num1 - num2;
        return Integer.toBinaryString(difference);
    }

    // Method to perform binary multiplication
    public static String multiplyBinary(String a, String b) {
        int num1 = Integer.parseInt(a, 2);
        int num2 = Integer.parseInt(b, 2);
        int product = num1 * num2;
        return Integer.toBinaryString(product);
    }

    // Method to perform binary division
    public static String divideBinary(String a, String b) {
        int num1 = Integer.parseInt(a, 2);
        int num2 = Integer.parseInt(b, 2);
        if (num2 == 0) {
            return "Error: Division by zero!";
        }
        int quotient = num1 / num2;
        return Integer.toBinaryString(quotient);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input binary numbers
        System.out.println("Enter first binary number: ");
        String firstBinary = scanner.nextLine();

        System.out.println("Enter second binary number: ");
        String secondBinary = scanner.nextLine();

        // Input operation choice
        System.out.println("\nSelect an operation:");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");

        int choice = scanner.nextInt();

        String result = "";

        switch (choice) {
            case 1:
                result = addBinary(firstBinary, secondBinary);
                break;
            case 2:
                result = subtractBinary(firstBinary, secondBinary);
                break;
            case 3:
                result = multiplyBinary(firstBinary, secondBinary);
                break;
            case 4:
                result = divideBinary(firstBinary, secondBinary);
                break;
            default:
                result = "Invalid operation!";
        }

        System.out.println("\nThe result is: " + result);
    }
}

Explanation of the Program

The program is structured into a single class named BinaryCalculator which includes the following methods:

  • addBinary: Takes two binary strings, converts them to integers, adds them, and returns the sum as a binary string.
  • subtractBinary: Similar to the addition method but performs subtraction instead.
  • multiplyBinary: Converts the binary strings to integers, multiplies them, and returns the result as a binary string.
  • divideBinary: Converts the binary strings to integers, divides them, and returns the quotient in binary form. It also handles division by zero.
  • main: This is the entry point of the program. It takes user input for two binary numbers and an operation, then calls the appropriate method to perform the operation and prints the result.

How to Run the Program

To run the Binary Calculator program, follow these steps:

  1. Make sure you have the Java Development Kit (JDK) installed on your system.
  2. Save the Java code into a file named BinaryCalculator.java.
  3. Open a terminal/command prompt and navigate to the directory where the file is saved.
  4. Compile the Java file using the following command:
    javac BinaryCalculator.java
  5. Run the compiled program using the command:
    java BinaryCalculator
  6. Follow the prompts to enter binary numbers and choose an arithmetic operation.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

One thought on “Binary Calculator in Java”
  1. Hello very nice blog!! Guy .. Excellent .. Amazing ..
    I’ll bookmark your blog and take the feeds also?
    I am happy to seek out a lot of helpful information here in the put up, we’d like develop extra strategies
    on this regard, thank you for sharing. . . . . .

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)