Skip to content

Commit

Permalink
Merge pull request tarunsinghofficial#404 from raksharaj83/patch-3
Browse files Browse the repository at this point in the history
PascalTriangle
  • Loading branch information
tarunsinghofficial committed Oct 19, 2020
2 parents 7315e25 + 19c3480 commit 43a39db
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Java_Programs_for_beginners/PascalTriangle
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
}

0 comments on commit 43a39db

Please sign in to comment.