Statement exercise question 10, statement exercise question
Print the Yang Hui triangle. The number of printed rows is input by the user.
1 then SC = new then (System. in); 2 System. out. println ("Enter the number of printed rows:"); 3 int row = SC. nextInt (); 4 int [] [] arr = new int [row] [row]; // defines a two-dimensional array 5 for (int I = 0; I <row; I ++) {6 for (int j = 0; j <= I; j ++) {7 // when the first column and the number of columns are equal to the number of rows, the corresponding element = 1 8 if (j = 0 | j = I) {9 arr [I] [j] = 1; 10} else {11 // element at another position = the sum of the last and top-left elements corresponding to this position 12 arr [I] [j] = arr [I-1] [j] + arr [I-1] [J-1]; 13} 14} 15} 16 // print the output 17 for (int I = 0; I <row; I ++) {18 for (int j = 0; j <= I; j ++) {19 System. out. print (arr [I] [j] + "\ t"); 20} 21 System. out. println (); 22}
Result: