forked from hmkcode/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascalstriangle.java
More file actions
29 lines (21 loc) · 819 Bytes
/
pascalstriangle.java
File metadata and controls
29 lines (21 loc) · 819 Bytes
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
public class PascalTriangleInJava {
public static void main(String[] args) {
System.out.println("Please enter number of rows of Pascal's triangle");
try (Scanner scnr = new Scanner(System.in)) {
int rows = scnr.nextInt();
System.out.printf("Pascal's triangle with %d rows %n", rows);
printPascalTriangle(rows);
}
}
public static void printPascalTriangle(int rows) {
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.printf("%" + (rows - i) * 2 + "s", "");
for (int j = 0; j <= i; j++) {
System.out.printf("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}