Generate Pascal’s Triangle in C#

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Binomial pattern

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 int to long.

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.

Enter rows between 1 and 14.

Live result
Press “Draw triangle”.

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

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
1

Multiplicative coefficient method (reference)

Classic interview solution: nested loops, leading spaces, and incremental binomial coefficients — matches the reference program for 5 rows.

c#
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

  1. The outer loop selects row index i (0-based).
  2. The space loop indents row i so the triangle appears centered.
  3. The inner coefficient loop prints C(i,j) by updating in place — no factorial function needed.
2

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.

c#
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

A triangular grid of numbers where each row starts and ends with 1, and every inner value equals the sum of the two numbers directly above it.
On the border there is only one parent path in the combinatorics view, so the coefficient is always 1.
C(n,k) counts unordered selections of k items from n. Pascal's Triangle lists these values row by row.
It updates the binomial coefficient in O(1) per step without computing factorials — faster and avoids huge intermediate products.
Yes. Row 20 already exceeds int limits for some entries. Use long or BigInteger for bigger triangles.
Printing r rows takes O(r^2) time because row i has i+1 values.

🔄 Input / output examples

numRowsFirst lines of output
11
31 / 1 1 / 1 2 1
5Full triangle ending with 1 4 6 4 1

Edge cases and pitfalls

Rows = 0

Empty triangle

Decide whether to print nothing or treat 0 as invalid input when reading from the user.

Overflow

Large row counts

C(34,17) exceeds int.MaxValue — use long or BigInteger.

Integer division

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 r rows; watch overflow on large r.
Did you know?

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”).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful