-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascalTriangle
More file actions
44 lines (40 loc) · 959 Bytes
/
pascalTriangle
File metadata and controls
44 lines (40 loc) · 959 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
System.out.println("Enter the number of the line ");
Scanner sc=new Scanner(System.in);
int line=sc.nextInt();
pascal(line);
}
static int fact(int num){
int f=1;
for(int i=1;i<=num;i++){
f*=i;
}
return f;
}
static int combi(int n, int r){
int com=fact(n)/(fact(r)*fact(n-r));
return com;
}
static void pascal(int line){
for(int i=1;i<=line;i++){
int k=1;
int r=0;
for(int j=1;j<line*2;j++){
if(j>=line-i+1 && j<=line+i-1 && k==1){
System.out.print(combi(i-1,r));
k=0;
r++;
}
else{
System.out.print(" ");
k=1;
}
}
System.out.print("\n");
}
}
}