C# program to Illustrate Upper Triangular Matrix

An upper triangular matrix is a square matrix where all elements below the main diagonal are zero. In an upper triangular matrix, elements are preserved only on or above the main diagonal (where row index ? column index).

Upper Triangular Matrix 1 2 3 4 0 5 6 7 0 0 8 9 0 0 0 10 Zero below diagonal Non-zero on/above diagonal

Logic

To display an upper triangular matrix, we use the condition i <= j where i is the row index and j is the column index. When this condition is true, we display the original matrix element; otherwise, we display zero −

if (i <= j)
   Console.Write(matrix[i, j] + "\t");  // display original element
else
   Console.Write("0\t");               // display zero

Example

Here's a complete program that creates and displays an upper triangular matrix from a given 2D array −

using System;

class UpperTriangularMatrix {
   static void Main() {
      int[,] matrix = {
         {1, 2, 3, 4},
         {5, 6, 7, 8},
         {9, 10, 11, 12},
         {13, 14, 15, 16}
      };
      
      int rows = matrix.GetLength(0);
      int cols = matrix.GetLength(1);
      
      Console.WriteLine("Original Matrix:");
      DisplayMatrix(matrix, rows, cols, false);
      
      Console.WriteLine("\nUpper Triangular Matrix:");
      DisplayMatrix(matrix, rows, cols, true);
   }
   
   static void DisplayMatrix(int[,] matrix, int rows, int cols, bool upperTriangular) {
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < cols; j++) {
            if (upperTriangular && i > j) {
               Console.Write("0\t");
            } else {
               Console.Write(matrix[i, j] + "\t");
            }
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

Original Matrix:
1	2	3	4	
5	6	7	8	
9	10	11	12	
13	14	15	16	

Upper Triangular Matrix:
1	2	3	4	
0	6	7	8	
0	0	11	12	
0	0	0	16	

Using Different Matrix Sizes

Here's another example with a 3x3 matrix to show the pattern more clearly −

using System;

class Program {
   static void Main() {
      int[,] matrix = {
         {10, 20, 30},
         {40, 50, 60},
         {70, 80, 90}
      };
      
      Console.WriteLine("3x3 Upper Triangular Matrix:");
      for (int i = 0; i < 3; i++) {
         for (int j = 0; j < 3; j++) {
            if (i <= j) {
               Console.Write(matrix[i, j] + "\t");
            } else {
               Console.Write("0\t");
            }
         }
         Console.WriteLine();
      }
   }
}

The output of the above code is −

3x3 Upper Triangular Matrix:
10	20	30	
0	50	60	
0	0	90	

Key Properties

  • All elements below the main diagonal are zero

  • Elements on and above the main diagonal retain their original values

  • The condition i <= j determines which elements to display

  • Upper triangular matrices are commonly used in linear algebra operations

Conclusion

An upper triangular matrix displays elements only on or above the main diagonal, with all elements below the diagonal set to zero. This is achieved by checking if the row index is less than or equal to the column index (i <= j) before displaying each element.

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

611 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements