Statement exercise question 10, statement exercise question

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.