Sum of Two Arrays Elements in Java

Array Programs in Java – 7 | In the previous Java program, we find the average of an array. Now in this post, we will develop a program to find the sum of two arrays elements in Java.

Program Description:- Write a Java program to find the sum of two arrays elements.

To calculate the sum of two arrays element by element in Java both arrays must be of equal type and equal size. If they have different types or different sizes then we will get IllegalArgumentException. To solve this problem we have to create a third array of the same size and then store the sum of corresponding elements of the given arrays. 

Note that we can’t add two arrays that are of different types or incompatible types. Both arrays should be similar types or compatible with each other.

Example:-
array1[] = {10, 20, 30, 40, 50};
array2[] = {9, 18, 27, 36, 45};

The resultant array will be,
array3[] = {19, 38, 57, 76, 95};
And it was calculated as,
array3[] = {10+9, 20+18, 30+27, 40+36, 50+45};

Program to Find Sum of Two Arrays in Java

import java.util.Scanner;
import java.util.Arrays;

public class TwoArraySum {
  public static void main(String[] args) {

    // create Scanner class object
    Scanner scan = new Scanner(System.in);

    // take number of elements in both array
    System.out.print("Enter number of elements in first array: ");
    int array1size = scan.nextInt();
    System.out.print("Enter number of elements in second array: ");
    int array2size = scan.nextInt();

    // both array must have same number of elements
    if(array1size != array2size) {
      System.out.println("Both array must have "+
                      "same number of elements");
      return; // stop 
    }

    // declare three array with given size
    int array1[] = new int[array1size];
    int array2[] = new int[array1size];
    int array3[] = new int[array1size];

    // take input for array1 elements
    System.out.println("Enter first array elements: ");
    for (int i=0; i<array1.length; i++) {
      array1[i] = scan.nextInt();
    }

    // take input for array2 elements
    System.out.println("Enter second array elements: ");
    for (int i=0; i<array2.length; i++) {
      array2[i] = scan.nextInt();
    }

    // loop to iterate through the array
    for (int i=0; i<array3.length; i++) {
      // add array elements
      array3[i] = array1[i] + array2[i];
    }

    // display the third array
    System.out.println("Resultant Array: " 
        + Arrays.toString(array3));
  }
}

Output for the different test-cases:-

Enter number of elements in first array: 5
Enter number of elements in second array: 5
Enter first array elements:
10 20 30 40 50
Enter second array elements:
9 18 27 36 45
Resultant Array: [19, 38, 57, 76, 95]

Enter number of elements in first array: 7
Enter number of elements in second array: 3
Both array must have same number of elements

In the above program, first, we had created a Scanner class object to take input from the end-user. Then we asked the user to input the number of elements in both arrays. See more:- How to take input for an array from the end-user.

If the number of elements in both arrays is different then we can never find the sum of two arrays element by element. In that case, display the message, stop the execution of the main method, and send control back to the caller method.

Otherwise take both arrays as input values from the end-user and store them in an appropriate array. Now, iterate through these arrays and calculate the sum of arrays element by element and store them into the third array. 

Finally, display the resultant array. To display them you can use normally for loop or for-each loop, iterate through them and display the elements of the third. But we used Arrays.toString(array3) to do the same task. In the Arrays class, toString() method is overridden to display the content of the array rather than its class name and hashcode value.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *