-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd2LargeNumbers.java
More file actions
56 lines (39 loc) · 1.23 KB
/
Copy pathadd2LargeNumbers.java
File metadata and controls
56 lines (39 loc) · 1.23 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//Write a function to add two very large numbers that cannot be stored in int.( out of range of int)
public class add2LargeNumbers {
public void addLargeNumbers(String num1, String num2){
int min = (num1.length() < num2.length() ? num1.length() : num2.length());
int max = (num1.length() < num2.length() ? num2.length() : num1.length());
int n1[] = new int[max];
int n2[] = new int[max];
System.out.println("Max is: "+max + "Min is : "+min);
for(int i=0;i<num1.length();i++){
n1[i] = num1.charAt(num1.length() -1-i)-48;
}
System.out.println(num1);
for(int i=0;i<num2.length();i++){
n2[i] = num2.charAt(num2.length()-1-i)-48;
}
System.out.println(num2);
int carry = 0, k=0;
int sum[] = new int[max+1];
for(k=0;k<max;k++){
sum[k] = (n1[k] + n2[k] + carry) % 10;
if(n1[k]+n2[k]+carry >= 10){
carry = 1;
}
else
carry = 0;
}
sum[max] = carry;
System.out.println("\n");
//String result;
for(int j = max; j>=0;j--){
System.out.print(sum[j]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
add2LargeNumbers obj = new add2LargeNumbers();
obj.addLargeNumbers("111111111111", "22222222222222"); //numbers of your choice.
}
}