33,082 views
45 votes
45 votes
IN JAVA *public class LabProgram Write a program that reads two lists of integers from input into two arrays and outputs the sum of multiplying the corresponding list items. The program first reads an integer representing the length of each list, followed by two lists of integers.

Ex: If the input is:

3
1 2 3
3 2 1
the program calculates (1 * 3) + (2 * 2) + (3 * 1) and outputs

10
Ex: If the input is:

4
2 3 4 5
1 1 1 1
the program calculates (2 * 1) + (3 * 1) + (4 * 1) + (5 * 1) and outputs

14

User Neat Machine
by
3.0k points

1 Answer

21 votes
21 votes

import java.util.*;

class LabProgram {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

int length = s.nextInt();

int[][] list = new int[2][length];

int total=0;

for(int i=0;i<2;i++) {

for(int j=0;j<length;j++) {

list[i][j] = s.nextInt();

}

}

for(int i=0;i<length;i++) {

total+=(list[0][i])*(list[1][i]);

}

System.out.println("Total:"+total);

}

}

IN JAVA *public class LabProgram Write a program that reads two lists of integers-example-1
User GregK
by
2.8k points