C# Program to Illustrate Lower Triangular Matrix

A lower triangular matrix is a square matrix where all elements above the main diagonal are zero. In other words, for any element at position (i, j), if j > i, then the element is zero.

The main diagonal consists of elements where the row index equals the column index (i = j). Below the diagonal, elements can have any non-zero values.

Syntax

The condition to check if an element should be displayed or set to zero −

if (i >= j)
    // Display the actual element
else
    // Display zero

Structure of Lower Triangular Matrix

Lower Triangular Matrix a?? 0 0 a?? a?? 0 a?? a?? a?? ? Non-zero elements ? Zero elements

Example

The following example creates a 3x3 matrix and displays it as a lower triangular matrix −

using System;

class LowerTriangularMatrix {
    static void Main() {
        int[,] matrix = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        
        Console.WriteLine("Original Matrix:");
        DisplayMatrix(matrix, rows, cols, false);
        
        Console.WriteLine("\nLower Triangular Matrix:");
        DisplayMatrix(matrix, rows, cols, true);
    }
    
    static void DisplayMatrix(int[,] matrix, int rows, int cols, bool lowerTriangular) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (lowerTriangular && 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	

Lower Triangular Matrix:
1	0	0	
4	5	0	
7	8	9	

Using Predefined Matrix Values

Example

using System;

class Program {
    static void Main() {
        int[,] A = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
        
        int m = A.GetLength(0);
        int n = A.GetLength(1);
        
        Console.WriteLine("Lower Triangular Matrix:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i >= j)
                    Console.Write(A[i, j] + "\t");
                else
                    Console.Write("0\t");
            }
            Console.WriteLine();
        }
    }
}

The output of the above code is −

Lower Triangular Matrix:
10	0	0	
40	50	0	
70	80	90	

Checking if a Matrix is Lower Triangular

Example

using System;

class Program {
    static void Main() {
        int[,] matrix1 = {
            {1, 0, 0},
            {4, 5, 0},
            {7, 8, 9}
        };
        
        int[,] matrix2 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };
        
        Console.WriteLine("Matrix 1 is lower triangular: " + IsLowerTriangular(matrix1));
        Console.WriteLine("Matrix 2 is lower triangular: " + IsLowerTriangular(matrix2));
    }
    
    static bool IsLowerTriangular(int[,] matrix) {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        
        for (int i = 0; i < rows; i++) {
            for (int j = i + 1; j < cols; j++) {
                if (matrix[i, j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }
}

The output of the above code is −

Matrix 1 is lower triangular: True
Matrix 2 is lower triangular: False

Conclusion

A lower triangular matrix has all elements above the main diagonal set to zero. The condition i >= j determines whether to display the actual element or zero, making it useful for mathematical computations and matrix operations in linear algebra.

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

324 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements