In this tutorial you will learn about the Java Program to add two binary numbers and its application with practical example.
In this tutorial, we will learn to create a Java Program to Add two Binary Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
- inbuilt Function
Binary Number System.
A Number system in which numbers are represented by using only two digits (0 and 1) with a base 2 is called a binary number system.
Binary addition.
For Binary addition you must remember the following rules
The four rules of binary addition are:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 =10.
Just look at the table how we add values in Binary are shown.
x |
y |
x+y |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 (where 1 is carried over) |
Java Program to add two binary numbers
In this program we will learn to add two binary numbers.First of all we declare required variable and also initiates required values in variable. Next we take value from user at run time and then after we will add two binary given by user.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
?? java program to add two Binary numbers import java.util.Scanner; public class Binary_conv { public static void main(String[] args) { // Declaring variables long no1, no2; int i = 0, rem = 0; int[] sum = new int[20]; // Taking values form user.. Scanner in = new Scanner(System.in); System.out.print("Input first binary number: "); no1 = in.nextLong(); System.out.print("Input second binary number: "); no2 = in.nextLong(); // Condition of addition while (no1 != 0 || no2 != 0) { sum[i++] = (int)((no1 % 10 + no2 % 10 + rem) % 2); rem= (int)((no1 % 10 + no2 % 10 + rem) / 2); no1 = no1 / 10; no2 = no2 / 10; } if (rem != 0) { sum[i++] = rem; } --i; // Printing the result.. System.out.print("Sum of two binary numbers is= : "); while (i >= 0) { System.out.print(sum[i--]); } System.out.print("\n"); } } |
Output
In the above program, we have first declared and initialized a set variables required in the program.
- no1 = it will hold first given binary number.
- no2= it will hold second Binary number.
- sum= it will hold sum of numbers.
- rem= ot will hold reminder of values.
First of all we declared and initialized set of variables required in the program. Then we will take a value from user at run time as shown in image below.
After taking all the values for user we will perform Binary Addition using the following formula.
With in the while loop first of all we check the given number are greater than zero and after that with all the four rule we add two given number by user as shown below.
User enter the two binary numbers that we add bit by bit using while loop and storing the result in sum.
And finally we will print the result.