0
0
CsharpProgramBeginner · 2 min read

C# Program to Print Pascal Triangle with Output

You can print Pascal's Triangle in C# by using nested loops and calculating each value with value = value * (row - col + 1) / (col + 1) inside the loops to generate each row.
📋

Examples

Input5
Output1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Input1
Output1
Input0
Output
🧠

How to Think About It

To print Pascal's Triangle, think of each row as a list of numbers where each number is the sum of the two numbers above it. Start with 1 at the top. For each row, calculate each number using the formula that relates it to the previous number in the same row, so you don't have to calculate factorials.
📐

Algorithm

1
Get the number of rows to print.
2
For each row from 0 to number of rows - 1:
3
Start with the first value as 1.
4
Print the first value.
5
For each position in the row from 1 to current row index:
6
Calculate the next value using the previous value multiplied by (row - position + 1) divided by position.
7
Print the calculated value.
8
Move to the next line after each row.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            int val = 1;
            for (int j = 0; j <= i; j++) {
                Console.Write(val + " ");
                val = val * (i - j) / (j + 1);
            }
            Console.WriteLine();
        }
    }
}
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
🔍

Dry Run

Let's trace printing 3 rows of Pascal's Triangle through the code

1

Start first row (i=0)

val = 1; print 1; no more positions; move to next line

2

Start second row (i=1)

val = 1; print 1; calculate val = 1 * (1 - 0) / (0 + 1) = 1; print 1; move to next line

3

Start third row (i=2)

val = 1; print 1; val = 1 * (2 - 0) / (0 + 1) = 2; print 2; val = 2 * (2 - 1) / (1 + 1) = 1; print 1; move to next line

Row (i)Position (j)Value (val)Printed
0011
1011
1111
2011
2122
2211
💡

Why This Works

Step 1: Start each row with 1

Each row in Pascal's Triangle starts with the number 1 because it represents the edge of the triangle.

Step 2: Calculate next values using previous

Each next number in the row is calculated by multiplying the previous number by (row - position) and dividing by (position + 1), which avoids factorial calculations.

Step 3: Print values and move to next line

After printing all values in a row, move to the next line to print the next row, building the triangle shape.

🔄

Alternative Approaches

Using factorial function
csharp
using System;
class Program {
    static int Factorial(int n) {
        int fact = 1;
        for (int i = 2; i <= n; i++) fact *= i;
        return fact;
    }
    static int Combination(int n, int r) {
        return Factorial(n) / (Factorial(r) * Factorial(n - r));
    }
    static void Main() {
        int rows = 5;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j <= i; j++) {
                Console.Write(Combination(i, j) + " ");
            }
            Console.WriteLine();
        }
    }
}
This method uses factorials to calculate combinations but is less efficient due to repeated factorial calculations.
Using 2D array to store values
csharp
using System;
class Program {
    static void Main() {
        int rows = 5;
        int[][] triangle = new int[rows][];
        for (int i = 0; i < rows; i++) {
            triangle[i] = new int[i + 1];
            triangle[i][0] = 1;
            triangle[i][i] = 1;
            for (int j = 1; j < i; j++) {
                triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j];
            }
        }
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j <= i; j++) {
                Console.Write(triangle[i][j] + " ");
            }
            Console.WriteLine();
        }
    }
}
This approach stores all values in a 2D array, which uses more memory but makes it easy to access any value later.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops where the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.

Space Complexity

The program uses only a few variables and prints values directly without storing the entire triangle, so space is O(1).

Which Approach is Fastest?

The direct calculation using the formula is fastest and uses less memory compared to factorial or 2D array methods.

ApproachTimeSpaceBest For
Formula calculationO(n^2)O(1)Fast and memory efficient
Factorial methodO(n^3)O(1)Simple but slower due to factorials
2D array storageO(n^2)O(n^2)Easy access to all values later
💡
Use the formula val = val * (row - col) / (col + 1) to efficiently calculate Pascal's Triangle values without factorials.
⚠️
Beginners often try to calculate factorials repeatedly, which slows down the program unnecessarily.