-
Notifications
You must be signed in to change notification settings - Fork 0
/
longnumberaddition.java
57 lines (46 loc) · 1.26 KB
/
longnumberaddition.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
51
52
53
54
55
56
57
import java.io.*;
import java.util.*;
// Read only region start
class UserMainCode
{
public String addNumberStrings(String input1,String input2){
// Read only region end
// Write code here...
if (input1.length() > input2.length()){
String t = input1;
input1 = input2;
input2 = t;
}
// Take an empty String for storing result
String str = "";
// Calculate lenght of both String
int n1 = input1.length(), n2 = input2.length();
// Reverse both of Strings
input1=new StringBuilder(input1).reverse().toString();
input2=new StringBuilder(input2).reverse().toString();
int carry = 0;
for (int i = 0; i < n1; i++)
{
// Do school mathematics, compute sum of
// current digits and carry
int sum = ((int)(input1.charAt(i) - '0') +
(int)(input2.charAt(i) - '0') + carry);
str += (char)(sum % 10 + '0');
// Calculate carry for next step
carry = sum / 10;
}
// Add remaining digits of larger number
for (int i = n1; i < n2; i++)
{
int sum = ((int)(input2.charAt(i) - '0') + carry);
str += (char)(sum % 10 + '0');
carry = sum / 10;
}
// Add remaining carry
if (carry > 0)
str += (char)(carry + '0');
// reverse resultant String
str = new StringBuilder(str).reverse().toString();
return str;
}
}