-
Notifications
You must be signed in to change notification settings - Fork 16
/
AddTwoLargeNumbers.java
50 lines (39 loc) · 1.34 KB
/
AddTwoLargeNumbers.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
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
public class AddTwoLargeNumbers {
static String findSum(String str1, String str2)
{
if (str1.length() > str2.length()){
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
str1 = new StringBuilder(str1).reverse().toString();
str2 = new StringBuilder(str2).reverse().toString();
int carry = 0;
for (int i = 0; i < n1; i++)
{
int sum = ((int)(str1.charAt(i) - '0') +
(int)(str2.charAt(i) - '0') + carry);
str += (char)(sum % 10 + '0');
carry = sum / 10;
}
for (int i = n1; i < n2; i++)
{
int sum = ((int)(str2.charAt(i) - '0') + carry);
str += (char)(sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0)
str += (char)(carry + '0');
str = new StringBuilder(str).reverse().toString();
return str;
}
public static void main(String[] args)
{
String str1 = "198212156835";
String str2 = "198111523228";
System.out.println("Sum of " + str1 + " and " + str2 + " = " + findSum(str1, str2)); // the ans should be = 396323680063
}
}