-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP21_PascalsTriangle.java
More file actions
39 lines (34 loc) · 1.07 KB
/
Copy pathP21_PascalsTriangle.java
File metadata and controls
39 lines (34 loc) · 1.07 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
package programs;
/**
* ============================================================
* PROGRAM 21: Pascal's Triangle
* ============================================================
* Problem: WAP to print Pascal's Triangle for N rows.
* - Formula: C(n, k) = C(n, k-1) * (n - k + 1) / k
*
* Example (n = 5):
* 1
* 1 1
* 1 2 1
* 1 3 3 1
* 1 4 6 4 1
* ============================================================
*/
public class P21_PascalsTriangle {
public static void printPascalsTriangle(int rows) {
System.out.println("=== PASCAL'S TRIANGLE (rows=" + rows + ") ===");
for (int i = 0; i < rows; i++) {
// Leading spaces
for (int s = 0; s < rows - i - 1; s++) System.out.print(" ");
int val = 1;
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", val);
val = val * (i - j) / (j + 1);
}
System.out.println();
}
}
public static void main(String[] args) {
printPascalsTriangle(6);
}
}