Generate Pascal’s Triangle in C#
What you’ll learn
- What Pascal’s Triangle is and how each inner cell is the sum of two cells above.
- How to print 5 rows using the multiplicative coefficient formula from the reference.
- An alternative additive approach that builds each row from the previous row.
- Live preview, complexity notes, and when to switch from
inttolong.
Overview
Pascal’s Triangle is a famous number pattern named after Blaise Pascal. Row 0 is 1; each later row starts and ends with 1, and interior values are sums of the two numbers above — or equivalently, binomial coefficients.
Multiplicative method
Update each coefficient with c = c * (i - j) / (j + 1) — no factorials.
Live preview
Pick row count (1–14) and draw the triangle in the browser.
Additive method
Store the previous row and sum neighbors — mirrors the “two above” definition.
Prerequisites
Nested for loops, integer arithmetic, and basic console output.
- Comfort with outer/inner loops where the inner bound depends on the outer index.
- Optional: one-dimensional arrays for the additive row method.
Understanding Pascal’s Triangle
Each number is the sum of the two directly above it (treat missing neighbors as zero). The first rows look like:
Row 0: 1 • Row 1: 1 1 • Row 2: 1 2 1 • Row 3: 1 3 3 1 • Row 4: 1 4 6 4 1. Entry at row n, column k equals C(n,k) — “n choose k.”
Mini triangle intuition
The 6 in row 4 comes from 3 + 3 above it. The edges stay 1 because there is only one path to each border cell.
Live preview
Uses the same multiplicative coefficient logic as Example 1.
Algorithm
Goal: print the first rows lines of Pascal’s Triangle.
Loop rows
Let i run from 0 to rows - 1. Row i has i + 1 values.
Print leading spaces
Indent with rows - i - 1 spaces so the triangle looks centered.
Compute coefficients
Start coefficient = 1; for each j, print it then update with coefficient * (i - j) / (j + 1).
📜 Pseudocode
for i from 0 to rows - 1:
print (rows - i - 1) spaces
coeff = 1
for j from 0 to i:
print coeff
coeff = coeff * (i - j) / (j + 1)
print newline Multiplicative coefficient method (reference)
Classic interview solution: nested loops, leading spaces, and incremental binomial coefficients — matches the reference program for 5 rows.
using System;
class Program
{
static void GeneratePascalsTriangle(int rows)
{
for (int i = 0; i < rows; i++)
{
for (int s = 0; s < rows - i - 1; s++)
Console.Write(" ");
int coefficient = 1;
for (int j = 0; j <= i; j++)
{
Console.Write($"{coefficient} ");
coefficient = coefficient * (i - j) / (j + 1);
}
Console.WriteLine();
}
}
static void Main()
{
int numRows = 5;
GeneratePascalsTriangle(numRows);
}
} How the program works
- The outer loop selects row index
i(0-based). - The space loop indents row
iso the triangle appears centered. - The inner coefficient loop prints
C(i,j)by updating in place — no factorial function needed.
Build each row from the previous row
This version mirrors the definition “sum of two above” using arrays. long gives a bit more headroom before overflow.
using System;
class Program
{
static void GeneratePascalsTriangleAdditive(int rows)
{
long[] previous = new long[rows];
long[] current = new long[rows];
for (int i = 0; i < rows; i++)
{
current[0] = 1;
for (int j = 1; j < i; j++)
current[j] = previous[j - 1] + previous[j];
if (i > 0)
current[i] = 1;
for (int s = 0; s < rows - i - 1; s++)
Console.Write(" ");
for (int j = 0; j <= i; j++)
Console.Write($"{current[j],6} ");
Console.WriteLine();
for (int j = 0; j <= i; j++)
previous[j] = current[j];
}
}
static void Main()
{
GeneratePascalsTriangleAdditive(5);
}
} Optimization and variations
Avoid factorials. The multiplicative update is the standard efficient approach — the old factorial formula is mathematically equivalent but slower.
Wider types. Switch to long or BigInteger when rows grow — middle entries blow up quickly.
Return a 2D list. Interviews sometimes ask for the data structure, not console art — build List<List<int>> with the additive logic.
❓ FAQ
🔄 Input / output examples
numRows | First lines of output |
|---|---|
1 | 1 |
3 | 1 / 1 1 / 1 2 1 |
5 | Full triangle ending with 1 4 6 4 1 |
Edge cases and pitfalls
Empty triangle
Decide whether to print nothing or treat 0 as invalid input when reading from the user.
Large row counts
C(34,17) exceeds int.MaxValue — use long or BigInteger.
Order of operations
The update coefficient * (i - j) / (j + 1) must divide evenly at each step for correct binomial values.
⏱️ Time and space complexity
Printing r rows requires O(r²) time because row i prints i + 1 numbers. The multiplicative method uses O(1) extra space; the additive array method uses O(r) for row buffers.
Summary
- Pascal’s Triangle lists binomial coefficients in a triangular layout.
- Generate rows with nested loops — multiplicative or additive approach.
- Total work is O(r²) for
rrows; watch overflow on larger.
Each entry in Pascal’s Triangle is a binomial coefficient — row n, position k counts how many ways to choose k items from n (written C(n,k) or “n choose k”).
8 people found this page helpful
