-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwobinarystring.java
More file actions
33 lines (33 loc) · 1.08 KB
/
twobinarystring.java
File metadata and controls
33 lines (33 loc) · 1.08 KB
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
import java.util.Scanner;
public class twobinarystring {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter first binary string:");
String a = sc.nextLine();
System.out.println("Enter second binary string:");
String b = sc.nextLine();
String result = addBinary(a, b);
System.out.println("The sum of the binary strings is: " + result);
}
public static String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1, j = b.length() - 1,carry = 0;
while (i >= 0 || j >= 0) {
int sum = carry;
if (i >= 0) {
sum += a.charAt(i) - '0';
i--;
}
if (j >= 0) {
sum += b.charAt(j) - '0';
j--;
}
sb.append(sum % 2);
carry = sum / 2;
}
if (carry != 0) {
sb.append(carry);
}
return sb.reverse().toString();
}
}