Print Yang Hui triangles using Java

Source: Internet
Author: User

 PackageYang Hui triangle;ImportJava.util.Scanner; Public classTriangle {Private StaticScanner SCN;  Public Static voidMain (string[] args) {SCN=NewScanner (system.in); System.out.println ("Please enter data"); intn =Scn.nextint (); //define a two-dimensional array        int[] Array =New int[N][n]; //assign a value of 1 to the first and last column of any row of this two-dimensional array         for(intx = 0;x < Array.length;x + +) {Array [x][0] = 1;//the first column of any rowArray [x][x] = 1;//the last column of any row        }        //start with the second line         for(intx = 2;x < Array.length;x + +){            //start with the first column because the No. 0 column is already assigned a value             for(inty = 1;y <= x;y + +) {array [x][y]= Array[x-1][y-1] + array[x-1][y]; }        }        //after the assignment is complete, traverse the two-dimensional array         for(intx = 0;x < Array.length;x + +){             for(inty = 0;y <= x;y + +) {System.out.print (Array[x][y]+ "\ T");//\ t means 8 spaces.} System.out.println ();//line Break                    }    }}

The printing results are as follows:

Print Yang Hui triangles using Java

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.