Generate Pascal’s Triangle in C
What you’ll learn
- How the triangle is built: edges are 1, inner cells are sums of the two cells above.
- How to print a centered triangle with two nested loops and a clever coefficient update (no factorial function).
- A second style that only adds neighbors from the previous row—same math, different code path.
Overview
Picture row zero as just 1. Each next row grows wider; the numbers are the same ones that appear in algebra when you expand (a + b)n. Programs usually print a few rows for small n so everything fits on screen.
Coefficients
Row i, position j, is “i choose j” when you count from zero.
Live preview
Pick how many rows (capped) and see the triangle in the page.
Two ways
Multiplicative step or add-the-two-above.
Prerequisites
for loops, nested loops, and integer division.
#include <stdio.h>,printf,int main(void).- Optional: curiosity about combinations “n choose k”—we explain just enough on this page.
The idea
Start with 1 at the top. Every new row copies 1 on both ends. Every inside slot is “what was above-left” plus “what was above-right”—that rule alone builds the whole triangle.
Computers can either mimic that addition pattern or jump between entries using a shortcut formula so you never store the whole triangle.
First five rows
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1Notice 2 = 1 + 1, 3 = 1 + 2, 6 = 3 + 3, and so on.
Live preview
Choose how many rows to print (1–14 in this demo). Uses the same multiplicative rule as Example 1.
Algorithm
Goal: for each row i from 0 to rows - 1, print i + 1 binomial values with optional leading spaces so the shape looks centered.
Outer loop: rows
Row index i runs from 0 upward.
Inner loop: entries
Generate C(i, j) for j = 0 .. i using a running value or neighbor sums.
Spacing
Print spaces before each row so the triangle tips upward in monospace output.
📜 Pseudocode
for i from 0 to rows - 1:
print leading spaces for row i
coeff = 1
for j from 0 to i:
print coeff
coeff = coeff * (i - j) / (j + 1)
newline Multiplicative update (reference style)
Matches the classic printed layout: three spaces per indent step and %6d columns. numRows = 5 reproduces the sample output below.
#include <stdio.h>
void generatePascalsTriangle(int rows) {
for (int i = 0; i < rows; i++) {
int coefficient = 1;
for (int j = 0; j < rows - i - 1; j++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("%6d", coefficient);
coefficient = coefficient * (i - j) / (j + 1);
}
printf("\n");
}
}
int main(void) {
int numRows = 5;
generatePascalsTriangle(numRows);
return 0;
} Explanation
The inner update walks across row i without storing the whole triangle. Each new coefficient is the next binomial entry in that row.
Build each row from the row above
Same triangle for five rows: keep the previous row in prev[], fill cur[], then copy back. Uses long long for a bit more room before overflow.
#include <stdio.h>
#define MAXR 64
void generatePascalsTriangleAdditive(int rows) {
long long prev[MAXR] = {0};
long long cur[MAXR];
for (int i = 0; i < rows; i++) {
cur[0] = 1;
for (int j = 1; j < i; j++) {
cur[j] = prev[j - 1] + prev[j];
}
if (i > 0) {
cur[i] = 1;
}
for (int j = 0; j < rows - i - 1; j++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("%6lld", cur[j]);
}
printf("\n");
for (int j = 0; j <= i; j++) {
prev[j] = cur[j];
}
}
}
int main(void) {
int numRows = 5;
generatePascalsTriangleAdditive(numRows);
return 0;
} Explanation
This is the “sum of two above” definition turned directly into arrays. It needs O(rows) extra memory for two slices of a row, but the logic is easy to explain on a chalkboard.
Notes
Overflow. Entries grow quickly; for playground sizes use long long or limit rows.
Bigger triangles. For Project Euler–style tasks you might compute coefficients modulo a prime or use arbitrary-precision integers.
❓ FAQ
🔄 Input / output
Change numRows in either main. You can later swap in scanf("%d", &numRows) if you want user input.
Edge cases
Nothing to print
Guard at the start of the generator if you accept user input.
Only the tip
You should see a single 1 (plus whatever spacing your loops print).
⏱️ Time and space complexity
| Program | Time | Extra space |
|---|---|---|
| Example 1 (multiplicative) | O(rows2) prints | O(1) |
| Example 2 (additive rows) | O(rows2) | O(rows) for buffers |
Summary
- Rule: add two neighbors above, sides stay
1. - Code: nested loops plus either multiplicative steps or row buffers.
- Care: spacing and integer overflow on deep triangles.
Each entry in Pascal’s triangle is a binomial coefficient “n choose k”—the same numbers that show up in the expansion of (a + b)n.
8 people found this page helpful
