0

I would like to add two 10-digit numbers using 3 arrays. I wrote these lines:

import java.util.*;

public class addition {
   public static void main(String[] args){
      Scanner enter = new Scanner(System.in);
      int[] arr1= new int[10];
      int[] arr2 = new int[10];
      int [] result = new int [11];

      System.out.print("Enter the first array: ");
      for(int i=0; i<10; i++)
      {
         arr1[i]=enter.nextInt();
      }
      System.out.print("Enter the second array: ");
      for(int i=0; i<10; i++)
      {
         arr2[i]=enter.nextInt();
      }

      for(int i=0; i<10; i++)
      {
         int b;
         int c;
         int a = arr1[9-i]+ arr2[9-i];
         if(a>9){
            b = a%10;
            c = a/10;
            result[9-i] = b;
            result[10-i] += c;
         }
         result[9-i]=a;
      }
      System.out.print("Result: "); 
      for(int i=10; i>=0; i--)
      {
         System.out.print(result[i]);
      }
   }
}

But the program does not work correctly. The result is not true.

Console:

Enter the first array: 8
6
9
5
3
9
9
1
4
2
Enter the second array: 8
5
3
8
0
0
3
1
6
6

Result: 09103129414131216

What should I do?

4
  • 2
    There is an issue with your code - if it overflows more than one digit it will fail Commented Nov 23, 2017 at 9:26
  • 1
    ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Nov 23, 2017 at 9:26
  • Right now, you're only storing data in the result array when you add two digits that sum to more than 9; you also need to store a result digit when the two digits sum to 9 or less. Commented Nov 23, 2017 at 9:32
  • result[9-i]=a; should be in else part of your if (a>9) statement Commented Nov 23, 2017 at 9:37

2 Answers 2

3

There are 2 things to fix:

  1. You populate your arrays back to front which makes input counter-intuitive. In other words this loop:

    for(int i=0; i<10; i++) {
      arr1[i]=enter.nextInt();
    }
    

    should become:

    for(int i=9; i>=0; i--) {
      arr1[i]=enter.nextInt();
    }
    

    The same holds for arr2.

  2. The main if statement checking for carry, should become:

    if(a>9){
      b=a%10;
      c=a/10;
      result[9-i]=b;
      result[10-i]+=c;
    } else {
      result[9-i]=a;
    }
    

With these fixes, your code works.

extra

You could go a little further and make your carry calculation simpler (only because we are adding only 2 digits. With such assumption the "adder loop" becomes:

for(int i=0; i<10; i++) {
  int a = arr1[9-i] + arr2[9-i];
  if (a>9) {
    result[9-i] = a-10;
    result[10-i] += 1;
  } else {
    result[9-i] = a;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

When dealing with such kind of problem we need to keep in mind the carry which will be there.

To solve this issue we should start adding from the right of the array to left.Like we do in Addition of two Numbers in maths class.

we need to keep the track of carry.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.