Program to print Lower triangular and Upper triangular matrix of an array in C

In C, triangular matrices are special square matrices where elements are zero either above or below the main diagonal. This program demonstrates how to print both lower and upper triangular matrices from a given matrix.

Syntax

/* Lower Triangular: matrix[i][j] = 0 when j > i */
/* Upper Triangular: matrix[i][j] = 0 when i > j */

Triangular Matrix Types

Lower Triangular Matrix − A square matrix where all entries above the main diagonal are zero.

Lower Triangular 1 0 0 2 8 0 4 9 7

Upper Triangular Matrix − A square matrix where all entries below the main diagonal are zero.

Upper Triangular 1 4 1 0 6 4 0 0 1

Algorithm

For Lower Triangular Matrix:

  • If column index is greater than row index, set element to 0
  • Otherwise, keep the original element

For Upper Triangular Matrix:

  • If row index is greater than column index, set element to 0
  • Otherwise, keep the original element

Example

#include <stdio.h>

int main() {
    int rows, cols, r, c, matrix[10][10];
    
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    
    printf("Enter the matrix elements:<br>");
    for(r = 0; r < rows; r++) {
        for(c = 0; c < cols; c++) {
            scanf("%d", &matrix[r][c]);
        }
    }
    
    printf("\nOriginal Matrix:<br>");
    for(r = 0; r < rows; r++) {
        for(c = 0; c < cols; c++) {
            printf("%d\t", matrix[r][c]);
        }
        printf("<br>");
    }
    
    printf("\nLower Triangular Matrix:<br>");
    for(r = 0; r < rows; r++) {
        for(c = 0; c < cols; c++) {
            if(r >= c) {
                printf("%d\t", matrix[r][c]);
            } else {
                printf("0\t");
            }
        }
        printf("<br>");
    }
    
    printf("\nUpper Triangular Matrix:<br>");
    for(r = 0; r < rows; r++) {
        for(c = 0; c < cols; c++) {
            if(r <= c) {
                printf("%d\t", matrix[r][c]);
            } else {
                printf("0\t");
            }
        }
        printf("<br>");
    }
    
    return 0;
}
Enter the number of rows: 3
Enter the number of columns: 3
Enter the matrix elements:
1 2 3
4 5 6
7 8 9

Original Matrix:
1	2	3	
4	5	6	
7	8	9	

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

Upper Triangular Matrix:
1	2	3	
0	5	6	
0	0	9

Key Points

  • Triangular matrices work only with square matrices (rows = columns)
  • Main diagonal elements remain unchanged in both triangular forms
  • Condition r >= c creates lower triangular matrix
  • Condition r <= c creates upper triangular matrix

Conclusion

Triangular matrices are fundamental in linear algebra and numerical computing. This program efficiently converts any square matrix into its lower and upper triangular forms by conditionally displaying elements based on their position relative to the main diagonal.

Updated on: 2026-03-15T12:34:54+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements