Print lower triangular matrix pattern from given array in C Program.

Given a matrix of n x n, the task is to print that matrix in lower triangular pattern. A lower triangular matrix is a matrix which has elements below the principal diagonal including the principal diagonal elements, with all other elements set as zero.

Let's understand this with help of a diagram −

Original Matrix 1 2 3 4 5 6 7 8 9 Lower Triangular Matrix 1 0 0 4 5 0 7 8 9 Green: Elements below and on diagonal Red: Elements above diagonal (set to 0)

Syntax

void printLowerTriangular(int matrix[n][n], int size);

Algorithm

The algorithm iterates through each element of the matrix. If the element is above the principal diagonal (i < j), it prints 0; otherwise, it prints the original matrix element −

for (i = 0; i < n; i++)
    for (j = 0; j < n; j++)
        if (i < j)
            print 0
        else
            print matrix[i][j]

Example

#include <stdio.h>
#define n 3

void printLowerTriangular(int mat[n][n]) {
    int i, j;
    printf("Lower Triangular Matrix:<br>");
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (i < j)
                printf("0\t");
            else
                printf("%d\t", mat[i][j]);
        }
        printf("<br>");
    }
}

int main() {
    int mat[n][n] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    printf("Original Matrix:<br>");
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%d\t", mat[i][j]);
        }
        printf("<br>");
    }
    printf("<br>");
    
    printLowerTriangular(mat);
    
    return 0;
}
Original Matrix:
1	2	3	
4	5	6	
7	8	9	

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

Key Points

  • Elements where i < j are above the diagonal and set to 0
  • Elements where i >= j are on or below the diagonal and retain their original values
  • The principal diagonal consists of elements where i == j

Conclusion

Printing a lower triangular matrix pattern involves checking the position of each element relative to the principal diagonal. Elements above the diagonal are replaced with zeros while preserving elements on and below the diagonal.

Updated on: 2026-03-15T11:55:17+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements