In this tutorial you will learn about the Java Program to find Sum of Two Arrays Elements and its application with practical example.
Java Program to find Sum of Two Arrays Elements
In this tutorial, we will learn to create a Java program that will Find the Sum of Two Arrays Elements using Java programming.
Prerequisites:-
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- For loop in Java programming.
- Arithmetic operations in Java Programming.
Program description to Find Sum of two array Elements.
In this program, we will first take the input array size of both arrays then we will check the size of both arrays. Now we will take the elements of both the array. Then we will add all the elements of both the array using the for a loop. At last, we will print that sum of both the arrays element using the print function.
With the help of this program, we can Find the Sum of Two Arrays Elements.
Program Code:-
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 |
import java.util.Scanner;//Importing the package to take input. import java.util.Arrays;//Importing the package to use array. public class Add2ArrayElements { public static void main(String[] args) { // create Scanner class object Scanner sc = new Scanner(System.in); // take number of elements in for first array System.out.print("Enter number of elements in first array: "); int arry1size = sc.nextInt(); // take number of elements in for first array System.out.print("Enter number of elements in second array: "); int arry2size = sc.nextInt(); // both array must have same number of elements if(arry1size != arry2size) { System.out.println("Both array must have "+ "same number of elements"); return; // stop } // declare three array with given size int arry1[] = new int[arry1size]; int arry2[] = new int[arry1size]; int arry3[] = new int[arry1size]; // take input for arry1 elements System.out.println("Enter first array elements: "); for (int i=0; i<arry1.length; i++) { arry1[i] = sc.nextInt(); } // take input for arry2 elements System.out.println("Enter second array elements: "); for (int i=0; i<arry2.length; i++) { arry2[i] = sc.nextInt(); } // loop to iterate through the array for (int i=0; i<arry3.length; i++) { // add array elements arry3[i] = arry1[i] + arry2[i]; } // display the third array System.out.println("Resultant Array: " + Arrays.toString(arry3)); } } |
Output:-
In the above program, we have first initialized the required variable.
- arry1[] = it will hold the integer value of the input elements.
- arry2[] = it will hold the integer value of the input elements.
- i = it will hold the integer value.
- arry3[] = it will hold the integer value sum of the array.
Input size of the array 1 and 2 from the user.
Taking the elements of the array from the user.
Program Code to find the sum of two array elements.
Printing output sum of the two array elements.