# Java Program to Add Two Binary Numbers

In this post, we will learn How to Add Two Binary Numbers in Java Programming Language. Let’s understand Binary Numbers and Binary Number Addition, then we will code the Java Program to Add Two Binary Numbers.

Contents

## What is a Binary Number ?

A Binary Number is a number that is expressed in the base of 2 numeral systems or binary numeral systems. A Binary Number is made up of only 0s and 1s. e.g. 1010101 , 1111, 1010, etc.

Binary Numbers are added using 4 rules of Binary Addition. By using those rules it is very easy to add two binary numbers. The 4 rules of Binary Number Addition are:

• 0 + 0 = 0
• 0 + 1 = 1
• 1 + 0 = 1
• 1 + 1 = 0, with a carry 1

For Example,
1 0 0 1 + 1 1 0 1 = 1 0 1 1 0

Now, let’s apply these 4 rules of binary addition and code the Java Program to Add Two Binary Numbers.

## Java Program to Add Two Binary Numbers

```import java.util.*;

public class Main{

//Method to add two binary numbers
public static int addBinary(int n1, int n2){

int sum = 0;
int carry = 0;
int powerOfTen = 1; // 10^0

while(n1 > 0 || n2 > 0 || carry > 0){

int digit1 = n1 % 10;
n1 = n1 / 10;

int digit2 = n2 % 10;
n2 = n2 / 10;

int digit = digit1 + digit2 + carry;

carry = digit / 2;
digit = digit % 2;

sum = sum + digit * powerOfTen;

powerOfTen = powerOfTen * 10;
}

return sum;
}

//main method
public static void main(String[] args){

Scanner sc = new Scanner(System.in);

System.out.println("Enter 1st Binary number: ");
int n1 = sc.nextInt();

System.out.println("Enter 2nd Binary number: ");
int n2 = sc.nextInt();

System.out.println("Sum of both Binary numbers : "+sum);

}

}```

Output

``````Enter 1st Binary number: 1001

Enter 2nd Binary number: 1101

Sum of both Binary numbers : 10110``````

## How Does This Program Work ?

In this program, we have taken the input of the two binary numbers from the user using the Scanner Class in Java. Then, we have created a method `addBinary(int n1, int n2)` which takes two parameters i.e. binary number 1 & binary number 2 and returns the sum of both numbers.

```    //Method to add two binary numbers
public static int addBinary(int n1, int n2){

int sum = 0;
int carry = 0;
int powerOfTen = 1; // 10^0

while(n1 > 0 || n2 > 0 || carry > 0){

int digit1 = n1 % 10;
n1 = n1 / 10;

int digit2 = n2 % 10;
n2 = n2 / 10;

int digit = digit1 + digit2 + carry;

carry = digit / 2;
digit = digit % 2;

sum = sum + digit * powerOfTen;

powerOfTen = powerOfTen * 10;
}

return sum;
}```

In this program, we have used the four rules of Binary Number Addition and made the logic of binary addition in Java. We are adding each digit one by one and also maintaining the carry.

```        int sum = addBinary(n1,n2);

System.out.println("Sum of both Binary numbers is "+sum);```

Then, we received the sum returned by the addBinary function and display the sum. This is the Java Program to Add Two Binary Numbers.

## Conclusion

I hope after going through this post, you understand how to code Java Program to Add Two Binary Numbers.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.