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