Java program to add two binary numbers
In this post, you will learn how to add two binary numbers using the Java programming language.
A binary number consists of two numbers, 0s and 1s. It is expressed in the base-2 numeral system or binary numeral system. As the computer only understands binary language, that is 0 or 1, all inputs given to a computer are decoded by it into a series of 0's or 1's to process them further. Any combination of 0 and 1 is a binary number, such as 1011, 100, 101011, 111011, etc.
Add two binary numbers in Java using while loop
In the given Java program, we are getting inputs from the user using the Scanner class, and then we are adding them bit by bit using the while loop.
import java.util.Scanner;
public class BinaryAddition {
public static void main(String[] args)
{
// variables to hold two input binary numbers
long bin1, bin2;
int i = 0, carry = 0;
// initializa an array sum to hold output binary number
int[] sum = new int[10];
Scanner scanner = new Scanner(System.in);
//getting first binary number
System.out.print("Enter first binary number: ");
bin1 = scanner.nextLong();
//getting second binary number
System.out.print("Enter second binary number: ");
bin2 = scanner.nextLong();
//closing scanner
scanner.close();
while (bin1 != 0 || bin2 != 0)
{
sum[i++] = (int)((bin1 % 10 + bin2 % 10 + carry) % 2);
carry = (int)((bin1 % 10 + bin2 % 10 + carry) / 2);
bin1 = bin1 / 10;
bin2 = bin2 / 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 of the above code:
Enter first binary number: 10111
Enter second binary number: 110101
Output: 1001100
Simple Java program to add two binary numbers
In the given Java program, first we convert the given inputs into interger types and then add those integer values. Next, we use toBinaryString() to convert them back to a binary number.
import java.util.Scanner;
public class Example2 {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
System.out.println("Enter two binary numbers: ");
String b1 = sc.nextLine();
String b2 = sc.nextLine();
int num1 = Integer.parseInt(b1, 2);
int num2 = Integer.parseInt(b2, 2);
int output = num1 + num2;
System.out.print("\nThe sum: ");
System.out.print(Integer.toBinaryString(output));
}
}
Output of the above code:
Enter two binary numbers: 110011
110000
The sum: 1100011
Related Articles
Swapping of two numbers without using third variable in JavaJava program to find the greatest of three numbers
Java random number between 1 and 10
Sort array in ascending order Java
String reverse in Java
Count vowels in a string Java
Java compare two strings
Java string split multiple delimiters
Char array to string Java
Java find largest of three numbers
Vowel and Consonant program in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java