-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalTriangle.java
More file actions
40 lines (36 loc) · 1.11 KB
/
PascalTriangle.java
File metadata and controls
40 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package unit_06.Examples.Example_16;
public class PascalTriangle {
private int[][] triangle;
private int height;
public void setHigh(int height) {
this.height = height;
}
public void BuildTriangle() {
triangle = new int[height][];
triangle[0] = new int[1];
triangle[0][0] = 1;
int row = 1;
while (row < triangle.length) {
int lastRow[] = triangle[row - 1];
int newRow[] = new int[row + 1];
newRow[0] = 1;
newRow[newRow.length - 1] = 1;
for (int i = 1; i < newRow.length - 1; i++) {
newRow[i] = lastRow[i - 1] + lastRow[i];
}
triangle[row] = newRow;
row++;
}
}
public void display() {
for (int i = 0; i < triangle.length; i++) {
for (int j = 0; j < (triangle.length - i); j++) {
System.out.print(" ");
}
for (int j = 0; j < triangle[i].length; j++) {
System.out.print(" " + triangle[i][j]);
}
System.out.println();
}
}
}