java - how to add two 1-d array and arrange it using for loop? -


the user should input 10 integer values list1 , 10 integer values list2. program should add contents of list1 , list2 store sum list3. program should display horizontally values of list1, list2, , list3. use loops.

here output:

list1 : 1 3 2 5 7 8 5 6 9 4 list2 : 2 1 4 3 2 1 4 2 0 2 list3 : 3 4 6 8 9 9 9 8 9 6 

here code

import java.io.*; public class list {     public static void main(string[] args) {         int list1[] = new int[10];         int list2[] = new int[10];         int i, j, k, num = 0, num1 = 0, num2 = 0;         string input = " ";         bufferedreader in = new bufferedreader(new         inputstreamreader(system. in ));         (i = 0; < 10; i++) {             list1[i] = 0;         }         (j = 0; j < 10; j++) {             list2[j] = 0;         }         try {             (i = 0; < 10; i++) {                 system.out.print("input value list[" + + "] = ");                 input = in .readline();                 num = integer.parseint(input);                 list1[i] = num;             }             (j = 0; j < 10; j++) {                 system.out.print("input value list[" + j + "] = ");                 input = in .readline();                 num1 = integer.parseint(input);                 list2[j] = num1;             }         } catch (ioexception e) {}         system.out.print("list1: ");         (i = 0; < 10; i++) {             system.out.print(list1[i] + "\t");         }         system.out.println();         system.out.print("list2: ");         (j = 0; j < 10; j++) {             system.out.print(list2[j] + "\t");         }         system.out.println();         int list3[] = new int[10];         list3[0] = list1[0] + list2[0];         list3[1] = list1[1] + list2[1];         list3[2] = list1[2] + list2[2];         list3[3] = list1[3] + list2[3];         list3[4] = list1[4] + list2[4];         list3[5] = list1[5] + list2[5];         list3[6] = list1[6] + list2[6];         list3[7] = list1[7] + list2[7];         list3[8] = list1[8] + list2[8];         list3[9] = list1[9] + list2[9];         system.out.print("list3: ");         (k = 0; k < 10; k++) {             system.out.print(list3[k] + "\t");         }     } } 

my program working correctly need put third list in loop can me?

in place of using sequential addition in code...

list3[0] = list1[0] + list2[0]; list3[1] = list1[1] + list2[1]; list3[2] = list1[2] + list2[2]; list3[3] = list1[3] + list2[3]; list3[4] = list1[4] + list2[4]; list3[5] = list1[5] + list2[5]; list3[6] = list1[6] + list2[6]; list3[7] = list1[7] + list2[7]; list3[8] = list1[8] + list2[8]; list3[9] = list1[9] + list2[9]; 

use for() loop

for (int l = 0; l < list3.length; l++) {     list3[l] = list1[l] + list2[l];  } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

javascript - Passport with Express 4 -