1#include <stdio.h>
2int main() {
3 int rows, coef = 1, space, i, j;
4 printf("Enter the number of rows: ");
5 scanf("%d", &rows);
6 for (i = 0; i < rows; i++) {
7 for (space = 1; space <= rows - i; space++)
8 printf(" ");
9 for (j = 0; j <= i; j++) {
10 if (j == 0 || i == 0)
11 coef = 1;
12 else
13 coef = coef * (i - j + 1) / j;
14 printf("%4d", coef);
15 }
16 printf("\n");
17 }
18 return 0;
19}
20
1import java.util.Scanner;
2public class PascalsTriangleJava
3{
4 static int findFactorial(int number)
5 {
6 int factorial;
7 for(factorial = 1; number > 1; number--)
8 {
9 factorial *= number;
10 }
11 return factorial;
12 }
13 // here's the function to display pascal's triangle
14 static int printPascalTraingle(int num, int p)
15 {
16 return findFactorial(num) / (findFactorial(num - p) * findFactorial(p));
17 }
18 public static void main(String[] args)
19 {
20 int row, a, b;
21 System.out.println("Please enter number of rows: ");
22 Scanner sc = new Scanner(System.in);
23 row = sc.nextInt();
24 System.out.println("Here's is pascal's triangle: ");
25 for(a = 0; a < row; a++)
26 {
27 for(b = 0; b < row - a; b++)
28 {
29 System.out.print(" ");
30 }
31 for(b = 0; b <= a; b++)
32 {
33 System.out.print(" " + printPascalTraingle(a, b));
34 }
35 System.out.println();
36 }
37 sc.close();
38 }
39}