I am not getting the correct output for this method. I have looked at the BigInteger class, and even tried to implement, after rewriting for my instance variable, but it did not carry the numbers over to the next digit, or bring down the remainders.
Thanks in advance.
public void Add (int[]a,int[]b){
int sum = 0;
Swap(a,b);// makes b[] the smaller array
flip(a); // flips array
flip(b); // 123 becomes 321
int[] c = new int[a.length]; // variable to hold Answer....
int diff = 0;
for (int i = a.length-1; i >=0; i--){
sum = a[i] + b[i] + diff;
diff = 0;
if (sum > 9 && i > 0){
sum -= 10;
diff = 1;
}
c[i] = sum;
}
flip(c);
print(c);
Output:
Enter First Number: 123456789
Enter Second Number: 123456
123456789 + 123456 = 0005791416
Enter First Number: 123456789
Enter Second Number: 123456789
123456789 + 123456789 = 2468035719
Enter First Number: 123456
Enter Second Number: 123456789
123456 + 123456789 = .....
(ArrayIndexOutOfBoundsException)