C# Program to Print Pascal Triangle with Output
value = value * (row - col + 1) / (col + 1) inside the loops to generate each row.Examples
How to Think About It
Algorithm
Code
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(); } } }
Dry Run
Let's trace printing 3 rows of Pascal's Triangle through the code
Start first row (i=0)
val = 1; print 1; no more positions; move to next line
Start second row (i=1)
val = 1; print 1; calculate val = 1 * (1 - 0) / (0 + 1) = 1; print 1; move to next line
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 |
|---|---|---|---|
| 0 | 0 | 1 | 1 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 |
| 2 | 0 | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 2 | 2 | 1 | 1 |
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 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(); } } }
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(); } } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula calculation | O(n^2) | O(1) | Fast and memory efficient |
| Factorial method | O(n^3) | O(1) | Simple but slower due to factorials |
| 2D array storage | O(n^2) | O(n^2) | Easy access to all values later |