-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava Program to Print Star Pascal’s Triangle
More file actions
68 lines (53 loc) · 1.19 KB
/
Copy pathJava Program to Print Star Pascal’s Triangle
File metadata and controls
68 lines (53 loc) · 1.19 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Java Program to Print Pascal's Triangle
// Importing input output classes
import java.io.*;
// Main Class
public class GFG {
// Method 1
// To find factorial of a number
public int factorial(int a)
{
// Edge case
// Factorial of 0 is unity
if (a == 0)
// Hence return 1
return 1;
// else recursively call the function over the
// number whose facoial is to be computed
return a * factorial(a - 1);
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Declare and initialize number whose
// factorial is to be computed
int k = 4;
int a, b;
// Creating an object of GFG class
// in the main() method
GFG g = new GFG();
// iterating using nested for loop to traverse over
// matrix
// Outer for loop
for (a = 0; a <= k; a++) {
// Inner loop 1
for (b = 0; b <= k - a; b++) {
// Print white space for left spacing
System.out.print(" ");
}
// Inner loop 2
for (b = 0; b <= a; b++) {
// nCr formula
System.out.print(
" "
+ g.factorial(a)
/ (g.factorial(a - b)
* g.factorial(b)));
}
// By now, we are done with one row so
// a new line
System.out.println();
}
}
}