C Programming Code Examples C > Arrays and Matrices Code Examples program code to find upper triangular matrix program code to find upper triangular matrix 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. 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. #include <stdio.h> #define maxrows 3 #define maxcols 3 int main() { int array[maxrows][maxcols]; int row, col, isUpper; /* Input elements in matrix from user */ printf("Enter elements in matrix of size %dx%d: \n", maxrows, maxcols); for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; col++) { scanf("%d", &array[row][col]); } } /* Check Upper triangular matrix condition */ isUpper = 1; for(row=0; row<maxrows; row++) { for(col=0; col<maxcols; 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<maxrows; row++) { for(col=0; col<maxcols; col++) { printf("%d ", array[row][col]); } printf("\n"); } } else { printf("\nThe matrix is not Upper triangular matrix."); } return 0; }