Pascal's Triangle
Completed
Read n (1–12) and print the first n rows of Pascal's triangle using a 2D array.
Input
One integer.
Output
n rows.
Example
Input
5
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Editor
C
Hints
Each inner value is the sum of the two values above it.
Solution explanation
Row i, position j is the binomial coefficient C(i, j).
Show solution
#include <stdio.h>
int main() {
int n, t[12][12];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i) t[i][j] = 1;
else t[i][j] = t[i - 1][j - 1] + t[i - 1][j];
printf("%d%s", t[i][j], j < i ? " " : "\n");
}
}
return 0;
}