C Example Code2026-03-202 min read
Print Pascal's Triangle in C using binomial combinations formula and 2D dynamic arrays. Step-by-step coefficient calculations explained.
What is Pascal's Triangle?
Pascal's Triangle is a triangular array of binomial coefficients. Each number is the sum of the two numbers directly above it:
text (ISO Standard)C(n, k) = n! / (k! × (n - k)!)
Complete C Implementation
c (ISO Standard)#include <stdio.h> void printPascalsTriangle(int rows) { for (int i = 0; i < rows; i++) { /* Print leading spaces for triangular alignment */ for (int space = 0; space < rows - i - 1; space++) { printf(" "); } int val = 1; for (int j = 0; j <= i; j++) { printf("%4d", val); /* Compute next coefficient in O(1) time: val * (i - j) / (j + 1) */ val = val * (i - j) / (j + 1); } printf("\n"); } } int main() { int rows = 6; printf("Pascal's Triangle with %d rows:\n\n", rows); printPascalsTriangle(rows); return 0; }
Sample Output
text (ISO Standard)Pascal's Triangle with 6 rows: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1
Complexity Analysis
- Time Complexity:
O(rows²)— Iterates through nested loop of row elements. - Space Complexity:
O(1)— Computes terms on the fly using binomial ratios without extra memory.
Related Guides
- Calculate factorials in Factorial Program in C.
- Master 2D nested loops in Arrays in C Programming.