C Program to Print Upper Triangular Matrix

Here is the C program to print upper triangular matrix of a square matrix. The main diagonal of a square matrix divides it into two sections, one above the diagonal and the other one is below the diagonal. If all elements in lower-section consists of zeros, it is a upper-triangular matrix and If all elements in upper-block consists of zeros, it is a lower-triangular matrix.

An upper triangular matrix is a square matrix in which all the elements below the main diagonal are zero. The variable U is commonly used to represent a upper triangular matrix.

Example of Upper Triangular Matrix:
   1 2 3 4
   0 5 6 7
   0 0 8 9
   0 0 0 1

Points to remember
Let A be the input matrix and U be the Upper triangular matrix of A.
  • All the elements below major diagonal of U are zero.
    U[i,j] = 0, If i > j.
  • U[i,j] = A[i,j], If i <= j.
  • A diagonal matrix is both an upper as well as lower triangular matrix.

C program to print upper triangular matrix of a square matrix

This program, takes a square matrix as input from user and then prints the upper triangular matrix. It traverse the input matrix row wise(first all elements of a row from left to right, then jump to next row) using two for loops. For every element, it compares whether row index is greater than column index. If row index is greater than column index then it prints zero other wise prints the current element.

#include <stdio.h>

int main(){
    int rows, cols, size, rowCounter, colCounter;
    int inputMatrix[50][50];
    
    printf("Enter size square matrix\n");
    scanf("%d", &size);
    rows = cols = size;
    
    printf("Enter Matrix of size %dX%d\n", rows, cols);
    /*  Input matrix*/
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            scanf("%d", &inputMatrix[rowCounter][colCounter]);
        }
    }
    /* 
      Printing upper triangular matrix 
      L[i,j] = 0, If i > j and L[i,j] = l[i,j], If i <= j
     */
    printf("Upper triangular Matrix\n");
    for(rowCounter = 0; rowCounter < rows; rowCounter++){
        for(colCounter = 0; colCounter < cols; colCounter++){
            if(rowCounter > colCounter){
                /* Lower triangle element*/
                printf("%d ", 0);
            } else {
                /* Upper triagle element*/
                printf("%d ", inputMatrix[rowCounter][colCounter]);
            }
        }
        printf("\n");
    }
    
    return 0;
}
Output
Enter size square matrix
2
Enter Matrix of size 2X2
1 1
1 1 
Upper triangular Matrix
1 1
0 1
Enter size square matrix
3
Enter Matrix of size 3X3
1 2 3
4 5 6
7 8 9
Lower triangular Matrix
1 2 3
0 5 6
0 0 9

Properties of an upper triangular matrix
  • The sum of two upper triangular matrices is an upper triangular matrix..
  • The product of two upper triangular matrices is an upper triangular matrix.
  • The inverse of a upper triangular matrix is an upper triangular.
  • The eigenvalues of a upper triangular matrix are the diagonal elements.

Related Topics
C program to print lower triangular matrix
C Program to print a matrix diagonally
C Program to sum each row and column of matrix
C Program to find transpose of matrix
C program to add two matrix
C Program for matrix multiplication
C program to check string is palindrome
C program to convert lowercase string to uppercase
List of all C programs