- Write a java program for addition, subtraction, multiplication and division of two binary numbers.
In this java program, we will first take two binary numbers as input from user and convert it to decimal numbers using Integer.parseInt() method. Then we add, subtract, multiply and divide integers(corresponding to given binary numbers) and convert the result back to binary number using Integer.toBinaryString() method.
Java program to add subtract multiply and divide binary numbers
package com.tcc.java.programs; import java.util.Scanner; /** * Java Program for Binary number addition, subtraction, multiplication and * division */ public class BinaryArithmetics { public static void main(String[] args) { String bOne, bTwo; int iOne, iTwo; Scanner scanner = new Scanner(System.in); System.out.println("Enter First Binary Number"); bOne = scanner.next(); System.out.println("Enter Second Binary Number"); bTwo = scanner.next(); // Convert Binary numbers to decimal iOne = Integer.parseInt(bOne, 2); iTwo = Integer.parseInt(bTwo, 2); System.out.println("Sum = " + Integer.toBinaryString(iOne + iTwo)); System.out.println("Difference = " + Integer.toBinaryString(iOne - iTwo)); System.out.println("Product = " + Integer.toBinaryString(iOne * iTwo)); System.out.println("Quotient = " + Integer.toBinaryString(iOne / iTwo)); } }Output
Enter First Binary Number 1000 Enter Second Binary Number 1000 Sum = 10000 Difference = 0 Product = 1000000 Quotient = 1