add two binary numbers

Java Program to Add two Binary Numbers with Example

In this tutorial, you will learn about the java program to add two binary numbers with example.

In this tutorial, we will create a Java program that will add two binary numbers. Because the binary number system uses only two symbols, 0 and 1, binary numbers are made up of only 0’s and 1’s.

Example: Add two binary numbers in Java

In this program, we use Scanner to get user input (the user enters the two binary numbers that we need to add), and then we add them bit by bit using a while loop and store the result in an array.

public class JavaExample {
   public static void main(String[] args)
   {
	//Two variables to hold two input binary numbers	 
	long b1, b2;
	int i = 0, carry = 0;

	//This is to hold the output binary number
	int[] sum = new int[10];

	//To read the input binary numbers entered by user
	Scanner scanner = new Scanner(System.in);

	//getting first binary number from user
	System.out.print("Enter first binary number: ");
	b1 = scanner.nextLong();
	//getting second binary number from user
	System.out.print("Enter second binary number: ");
	b2 = scanner.nextLong();

	//closing scanner after use to avoid memory leak
	scanner.close();
	while (b1 != 0 || b2 != 0) 
	{
		sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2);
		carry = (int)((b1 % 10 + b2 % 10 + carry) / 2);
		b1 = b1 / 10;
		b2 = b2 / 10;
	}
	if (carry != 0) {
		sum[i++] = carry;
	}
	--i;
	System.out.print("Output: ");
	while (i >= 0) {
		System.out.print(sum[i--]);
	}
	System.out.print("\n");  
   }
}

Output:

Enter first binary number: 11100
Enter second binary number: 10101
Output: 110001

Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introdujbced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today. Annotations in Java are used to provide metadata for your Java code. Because they are metadata, Java annotations do not directly affect the execution of your code, though some types of annotations can. Java annotations were introduced in Java 5 and are still in use today.

In this tutorial, you will learn about the java program to add two numbers with example. In this tutorial, you will learn about the java program to add two numbers with example.

Leave a Reply