-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_Triangular.c
57 lines (50 loc) · 1.28 KB
/
Matrix_Triangular.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the matrix: ");
scanf("%d", &n);
int matrix[n][n];
int i, j;
// Read input matrix
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Enter element (%d, %d): ", i, j);
scanf("%d", &matrix[i][j]);
}
}
// Check if lower triangular
int is_lower_triangular = 1;
for (i = 1; i < n; i++) {
for (j = 0; j < i; j++) {
if (matrix[i][j] != 0) {
is_lower_triangular = 0;
break;
}
}
if (is_lower_triangular == 0) {
break;
}
}
// Check if upper triangular
int is_upper_triangular = 1;
for (i = 0; i < n; i++) {
for (j = i+1; j < n; j++) {
if (matrix[i][j] != 0) {
is_upper_triangular = 0;
break;
}
}
if (is_upper_triangular == 0) {
break;
}
}
// Print results
if (is_lower_triangular) {
printf("The matrix is lower triangular.\n");
} else if (is_upper_triangular) {
printf("The matrix is upper triangular.\n");
} else {
printf("The matrix is not lower or upper triangular.\n");
}
return 0;
}