-
Notifications
You must be signed in to change notification settings - Fork 0
/
PascalTriangle.java
56 lines (48 loc) · 1.1 KB
/
PascalTriangle.java
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
import java.io.*;
import java.util.*;
public class PascalTriangle {
public static int fact(int n)
{
if(n==0)
{
return 1;
}
else
{
int a = n*fact(n-1);
return a;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i =0; i<n; i++)
{
int space= n-i-1;
while(space>0)
{
System.out.print(" ");
space--;
}
int num= 2*i+1;
int index=1;
for(int j= num; j>0; j--)
{
if(j==1 || j==num)
{
System.out.print(1);
}
else if(j%2==0)
{
System.out.print(" ");
}
else
{
int a = fact(i)/ (fact(i-index) *fact(index++));
System.out.print(a);
}
}
System.out.println("");
}
}
}