Array Exercises
Write a C program to read elements in a matrix and check whether the matrix is upper triangular matrix or not. C program to check upper triangular matrix. Logic to find upper triangular matrix in C programming.
Required knowledge
Basic C programming, C for loop, ArrayS
Upper triangular matrix
Upper triangular matrix is a special square matrix whose all elements below the main diagonal is zero.

Logic to find upper triangular matrix
To check whether a matrix is upper triangular or not we need to check whether all elements below main diagonal are zero or not.
For any matrix A if all elements Aij = 0 (Where i ≥ j). Means, if(array[row][col] == 0) and row > col then it is upper triangular matrix.
Program to find upper triangular matrix
/** * C program to find upper triangular matrix */ #include#define MAX_ROWS 3 #define MAX_COLS 3 int main() { int array[MAX_ROWS][MAX_COLS]; int row, col, isUpper; /* Input elements in matrix from user */ printf("Enter elements in matrix of size %dx%d: \n", MAX_ROWS, MAX_COLS); for(row=0; row<MAX_ROWS; row++) { for(col=0; col>MAX_COLS; col++) { scanf("%d", &array[row][col]); } } /* Check Upper triangular matrix condition */ isUpper = 1; for(row=0; row<MAX_ROWS; row++) { for(col=0; col<MAX_COLS; col++) { /* * If elements below the main diagonal (col<row) * is not equal to zero then it is not upper triangular matrix */ if(col<row && array[row][col]!=0) { isUpper = 0; } } } /* Print elements of upper triangular matrix */ if(isUpper == 1) { printf("\nThe matrix is Upper triangular matrix.\n"); for(row=0; row<MAX_ROWS; row++) { for(col=0; col<MAX_COLS; col++) { printf("%d ", array[row][col]); } printf("\n"); } } else { printf("\nThe matrix is not Upper triangular matrix."); } return 0; }
Output
Enter elements in matrix of size 3x3:
10 20 30
0 50 60
0 0 90
The matrix is Upper triangular matrix.
10 20 30
0 50 60
0 0 90
Process returned 0 (0x0) execution time : 27.322 s
Enter elements in matrix of size 3x3:
10 20 30
40 50 60
0 0 90
The matrix is not Upper triangular matrix.
Process returned 0 (0x0) execution time : 20.445 s
Program to find upper triangular matrix
/** * C program to find upper triangular matrix */ #include <stdio.h> int main() { int array[10][10]; int row, col, isUpper,m,n; printf("Enter no of rows:"); scanf("%d",&m); printf("Enter no of columns:"); scanf("%d",&n); /* Input elements in matrix from user */ printf("Enter elements in matrix of size %dx%d: \n", m, n); for(row=0; row<m; row++) { for(col=0; col<n; col++) { scanf("%d", &array[row][col]); } } /* Check Upper triangular matrix condition */ isUpper = 1; for(row=0; row<m; row++) { for(col=0; col<n; col++) { /* * If elements below the main diagonal (col<row) * is not equal to zero then it is not upper triangular matrix */ if(col<row && array[row][col]!=0) { isUpper = 0; } } } /* Print elements of upper triangular matrix */ if(isUpper == 1) { printf("\nThe matrix is Upper triangular matrix.\n"); for(row=0; row<m; row++) { for(col=0; col<n; col++) { printf("%d ", array[row][col]); } printf("\n"); } } else { printf("\nThe matrix is not Upper triangular matrix."); } return 0; }