Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to generate pascals triangle for the given number using C#?
Pascal's Triangle is a triangular number pattern where each number is the sum of the two numbers directly above it. The triangle starts with 1 at the top, and each row begins and ends with 1. Pascal's Triangle has many applications in mathematics, statistics, and combinatorics, particularly for calculating binomial coefficients and combinations.
Each row represents the coefficients of the binomial expansion (a + b)^n, where n is the row number starting from 0. The triangle demonstrates beautiful mathematical properties and patterns that make it useful in various computational problems.
Algorithm Complexity
-
Time Complexity: O(N²) where N is the number of rows, as we need to calculate each element
-
Space Complexity: O(N²) to store all elements of the triangle
Using Nested Lists Approach
The most straightforward approach uses nested lists where each inner list represents a row of Pascal's Triangle −
using System;
using System.Collections.Generic;
public class PascalTriangle {
public List<List<int>> GeneratePascal(int numRows) {
List<List<int>> triangle = new List<List<int>>();
if (numRows <= 0) {
return triangle;
}
// First row is always [1]
triangle.Add(new List<int> { 1 });
// Generate remaining rows
for (int i = 1; i < numRows; i++) {
List<int> currentRow = new List<int>();
List<int> previousRow = triangle[i - 1];
// First element is always 1
currentRow.Add(1);
// Calculate middle elements
for (int j = 1; j < i; j++) {
currentRow.Add(previousRow[j - 1] + previousRow[j]);
}
// Last element is always 1
currentRow.Add(1);
triangle.Add(currentRow);
}
return triangle;
}
public void DisplayTriangle(List<List<int>> triangle) {
foreach (var row in triangle) {
Console.WriteLine("[" + string.Join(",", row) + "]");
}
}
}
class Program {
public static void Main(string[] args) {
PascalTriangle pt = new PascalTriangle();
var triangle = pt.GeneratePascal(5);
pt.DisplayTriangle(triangle);
}
}
The output of the above code is −
[1] [1,1] [1,2,1] [1,3,3,1] [1,4,6,4,1]
Using Array-Based Approach
For better performance and memory efficiency, you can use a 2D array approach −
using System;
public class PascalArray {
public int[,] GeneratePascalArray(int numRows) {
int[,] triangle = new int[numRows, numRows];
for (int i = 0; i < numRows; i++) {
// First and last elements of each row are 1
triangle[i, 0] = 1;
triangle[i, i] = 1;
// Calculate middle elements
for (int j = 1; j < i; j++) {
triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j];
}
}
return triangle;
}
public void DisplayArray(int[,] triangle, int numRows) {
for (int i = 0; i < numRows; i++) {
Console.Write("[");
for (int j = 0; j <= i; j++) {
Console.Write(triangle[i, j]);
if (j < i) Console.Write(",");
}
Console.WriteLine("]");
}
}
}
class Program {
public static void Main(string[] args) {
PascalArray pa = new PascalArray();
int numRows = 6;
var triangle = pa.GeneratePascalArray(numRows);
pa.DisplayArray(triangle, numRows);
}
}
The output of the above code is −
[1] [1,1] [1,2,1] [1,3,3,1] [1,4,6,4,1] [1,5,10,10,5,1]
Generating Single Row
Sometimes you only need a specific row of Pascal's Triangle. Here's an optimized approach using the binomial coefficient formula −
using System;
public class PascalRow {
public int[] GetRow(int rowIndex) {
int[] row = new int[rowIndex + 1];
row[0] = 1;
for (int i = 1; i <= rowIndex; i++) {
// Using the formula: C(n,k) = C(n,k-1) * (n-k+1) / k
row[i] = (int)((long)row[i - 1] * (rowIndex - i + 1) / i);
}
return row;
}
}
class Program {
public static void Main(string[] args) {
PascalRow pr = new PascalRow();
// Get the 4th row (0-indexed)
int[] row4 = pr.GetRow(4);
Console.WriteLine("Row 4: [" + string.Join(",", row4) + "]");
// Get the 6th row
int[] row6 = pr.GetRow(6);
Console.WriteLine("Row 6: [" + string.Join(",", row6) + "]");
}
}
The output of the above code is −
Row 4: [1,4,6,4,1] Row 6: [1,6,15,20,15,6,1]
Conclusion
Pascal's Triangle can be generated efficiently using nested lists, 2D arrays, or optimized single-row calculations. The choice of approach depends on whether you need the entire triangle or just specific rows, with array-based methods offering better memory efficiency for larger triangles.
