- Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathPascal_Triangle.java
More file actions
Latest commit
45 lines (37 loc) · 1.17 KB
/
Copy pathPascal_Triangle.java
File metadata and controls
45 lines (37 loc) · 1.17 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
41
42
43
44
45
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Each number in the triangle is the sum of the two numbers directly above it.
we take number of rows as input
And we know that to find any element at particular position (i.e at ith row and cth column) we can use the combination formula nCr.
nCr=factorial(n)/(factorial(r)*factorial(n-r))
*/
importjava.util.Scanner;
publicclassPascal_Triangle {
publicstaticlongfactorial(intn) {
longresult = 1;
for (intc = 1; c <= n; c++) {
result *= c;
}
returnresult;
}
publicstaticvoidmain(String[] args) {
Scannerscanner = newScanner(System.in);
System.out.println("Enter the number of rows you wish to see in Pascal's Triangle:");
intn = scanner.nextInt();
for (inti = 0; i < n; i++) {
for (intc = 0; c <= (n - i - 2); c++) {
System.out.print(" ");
}
for (intc = 0; c <= i; c++) {
longcoeff = factorial(i) / (factorial(c) * factorial(i - c));
System.out.print(coeff + " ");
}
System.out.println();
}
scanner.close();
}
}