It didnt work. My output now is:
Enter First Number: 123456789
Enter Second Number: 1
123456789 + 1 = 1234567810Enter First Number: 1
Enter Second Number: 123456789
1 + 123456789 = java.lang.ArrayIndexOutOfBoundsException: 1
Ok I got the mistake for 1st part
The code should be
public void Add (int[]a,int[]b)
{
int sum = 0;
if(a.length<b.length)
Swap(a,b);// makes b[] the smaller array
flip(a); // filps array
flip(b); // 123 becomes 321
int[] c = new int[a.length];
int[] d = new int[a.length];
for(int i=0;i<b.length;i++)
d[i]=b[i];
for(int i=b.length;i<a.length;i++)
d[i]=0;
int diff = 0;
for (int i = 0; i<a.length; i++){
sum = a[i] + d[i]+ diff;
diff = 0;
if (sum > 9 && i <=a.length-2){
sum -= 10;
diff = 1;
}
c[i] = sum;
}
flip(c);
printArray(c);
}
This will print the correct result. For second part, i.e. ArrayIndexOutofBoundException, I think you have a problem in Swap method. Kindly check your Swap method that it swaps the two array if the length of array1<array2.