Write a C program to check whether the given matrix is upper triangular or not.
#include <stdlib.h>
int main() {
int **mat, order, i , j;
/* get the order of the matrix from user */
printf("Enter the order of matrix:");
scanf("%d", &order);
/* dynamically allocate memory to store entries */
mat = (int **)malloc(sizeof (int) * order);
for (i = 0; i < order; i++)
mat[i] = (int *)malloc(sizeof (int) * order);
/* get the input matrix from the user */
printf("Enter your inputs:\n");
for (i = 0; i < order; i++)
for (j = 0; j < order; j++)
scanf("%d", &mat[i][j]);
/* check for upper triangulat matrix */
for (i = 0; i < order; i++) {
for (j = i - 1; j >= 0; j--) {
if (mat[i][j] != 0) {
printf("Not a upper triangular matrix\n");
exit(0);
}
}
}
/* print the output */
printf("Given matrix is a upper triangular matrix\n");
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the order of matrix:3
Enter your inputs:
10 20 30
0 30 10
0 0 11
Given matrix is a upper triangular matrix
Enter the order of matrix:3
Enter your inputs:
10 20 30
0 30 10
0 0 11
Given matrix is a upper triangular matrix
No comments:
Post a Comment