-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddingArray.java
More file actions
42 lines (37 loc) · 1.21 KB
/
Copy pathAddingArray.java
File metadata and controls
42 lines (37 loc) · 1.21 KB
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
import java.util.Scanner;
public class AddingArray {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the array length:");
int len=scan.nextInt();
// Creating new arrays arr1 and arr2
int arr1[] = new int[len];
int arr2[] = new int[len];
// Taking the input for arr1
System.out.println("Enter the arr1 elements:");
for (int i = 0; i <= arr1.length-1; i++) {
System.out.println("Enter an element");
arr1[i] = scan.nextInt();
}
// Taking the input for arr2
System.out.println("Enter the arr2 elements:");
for (int i = 0; i <= arr2.length-1; i++) {
System.out.println("Enter an element");
arr2[i] = scan.nextInt();
}
// Creating the result array
int result[] = new int[len];
int j=len-1;
// Adding the arr1 and arr2 elements and storing it in result array in reverse order
for (int i = 0; i <= arr1.length-1; i++) {
result[j] = arr1[i]+arr2[i];
j--;
}
// Displaying the contents of the result array
System.out.println();
System.out.println("Result array contents are");
for (int i = 0; i <= result.length-1; i++) {
System.out.print(result[i] + " ");
}
}
}