How to display numbers in the form of Triangle using C#?

To display numbers in the form of a triangle in C#, we use a two-dimensional array to store the triangle values and nested loops to generate the pattern. This creates what's known as Pascal's Triangle, where each number is the sum of the two numbers above it.

Syntax

Following is the syntax for declaring a two-dimensional array for the triangle −

int[,] array = new int[rows, columns];

Following is the pattern for Pascal's Triangle logic −

if (j == 0 || i == j) {
    a[i, j] = 1;  // edges are always 1
} else {
    a[i, j] = a[i-1, j] + a[i-1, j-1];  // sum of two above numbers
}

How Pascal's Triangle Works

Pascal's Triangle follows a specific pattern where each row starts and ends with 1, and each interior number is the sum of the two numbers directly above it from the previous row.

Pascal's Triangle Pattern 1 1 1 1 2 1 1 3 3 1 ? Edges = 1 ? Interior = sum above ? Addition lines

Using Basic Pascal's Triangle

Example

using System;

class Demo {
    public static void Main() {
        // two dimensional array
        int[,] a = new int[5, 5];

        for (int i = 0; i < 5; i++) {
            for (int k = 7; k > i; k--) {
                // prints spaces for triangle alignment
                Console.Write(" ");
            }

            // loop to generate and print the triangle
            for (int j = 0; j < i; j++) {
                if (j == 0 || i == j) {
                    a[i, j] = 1;
                } else {
                    a[i, j] = a[i - 1, j] + a[i - 1, j - 1];
                }
                Console.Write(a[i, j] + " ");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

       1 
      1 1 
     1 2 1 
    1 3 3 1 

Using Dynamic Row Count

Example

using System;

class PascalTriangle {
    public static void Main() {
        int rows = 6;
        int[,] triangle = new int[rows, rows];

        for (int i = 0; i < rows; i++) {
            // Print leading spaces
            for (int space = 0; space < rows - i - 1; space++) {
                Console.Write("  ");
            }

            // Generate and print triangle numbers
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    triangle[i, j] = 1;
                } else {
                    triangle[i, j] = triangle[i - 1, j - 1] + triangle[i - 1, j];
                }
                Console.Write(triangle[i, j].ToString().PadRight(4));
            }
            Console.WriteLine();
        }
    }
}

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   

Using Single Array Approach

Example

using System;

class TrianglePattern {
    public static void Main() {
        int rows = 5;
        
        for (int i = 0; i < rows; i++) {
            int[] currentRow = new int[i + 1];
            
            // Add spaces for alignment
            for (int space = 0; space < rows - i; space++) {
                Console.Write(" ");
            }
            
            // Calculate each element in current row
            for (int j = 0; j <= i; j++) {
                if (j == 0 || j == i) {
                    currentRow[j] = 1;
                } else {
                    // Use binomial coefficient formula
                    currentRow[j] = 1;
                    for (int k = 1; k <= j; k++) {
                        currentRow[j] = currentRow[j] * (i - k + 1) / k;
                    }
                }
                Console.Write(currentRow[j] + " ");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 

Conclusion

Creating number triangles in C# involves using two-dimensional arrays and nested loops to generate Pascal's Triangle patterns. The key concept is that edge elements are always 1, while interior elements are the sum of the two numbers directly above them in the previous row.

Updated on: 2026-03-17T07:04:35+05:30

300 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements