forked from shikaruki/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryAddition.java
39 lines (39 loc) · 901 Bytes
/
BinaryAddition.java
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
38
39
import java.util.Scanner;
public class BinaryAddition {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t!=0)
{
helperfunction(in);
t = in.nextInt();
}
}
public static void helperfunction(Scanner in)
{
long bin1, bin2;
int i = 0, carry = 0;
int[] sum = new int[10];
System.out.print("Enter first binary number: ");
bin1 = in.nextLong();
System.out.print("Enter second binary number: ");
bin2 = in.nextLong();
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");
}
}