forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaExample.java
More file actions
34 lines (31 loc) · 736 Bytes
/
Copy pathJavaExample.java
File metadata and controls
34 lines (31 loc) · 736 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
import java.util.Scanner;
public class JavaExample {
static int fact(int num) {
int factorial;
for(factorial = 1; num > 1; num--){
factorial *= num;
}
return factorial;
}
static int ncr(int n,int r) {
return fact(n) / ( fact(n-r) * fact(r) );
}
public static void main(String args[]){
int rows, i, j;
//getting number of rows from user
System.out.println("Enter number of rows:");
Scanner scanner = new Scanner(System.in);
rows = scanner.nextInt();
scanner.close();
System.out.println("Pascal Triangle:");
for(i = 0; i < rows; i++) {
for(j = 0; j < rows-i; j++){
System.out.print(" ");
}
for(j = 0; j <= i; j++){
System.out.print(" "+ncr(i, j));
}
System.out.println();
}
}
}