Wap,take 2 list from user. Both to be of the same length. Print the sum of list 1 and 2 in 3rd list. WAP
Answers
Answer:
//we will take the size of the list as 5..
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> total = new ArrayList<Integer>();
int sum = 0 , sum1 = 0 , sum2 = 0;
//loop to create first list
for(int i = 0; i < 5; i++){
System.out.print("Enter number " + (i+1) + " in the first list:");
int x = sc.nextInt();
list.add(x);
}
System.out.println("LIST 1 \n" + list);
//loop for second list
for(int i = 0; i < 5; i++){
System.out.print("Enter number " + (i+1) + " in the second list:");
int x = sc.nextInt();
list1.add(x);
}
System.out.println("LIST 2 \n" + list1);
//creating loops for finding the sum
for(int i = 0; i < list.size(); i++){
sum = sum + list.get(i);
}
for(int i = 0; i < list1.size(); i++){
sum1 = sum1 + list1.get(i);
}
sum2 = sum + sum1;
total.add(sum2); //adding the total sum to the third list
System.out.println("The sum of both the lists is : " + sum2);
}
}
Explanation: