forked from akn714/1st-yr-c-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.c
34 lines (26 loc) · 717 Bytes
/
27.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
// Task : WAP that finds the sum of diagonal elements of a mxn matrix
#include <stdio.h>
int main() {
int n, m, i, j, sum = 0;
// inputtting the order of the matrix
printf("enter order of matrix? ");
scanf("%d%d", &n, &m);
int mat[n][m];
// inputting the matrix
printf("enter matrix :\n");
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d", &mat[i][j]);
}
}
// finding the sum of diagonal elements of matrix
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(i==j){
sum += mat[i][i];
}
}
}
printf("sum of diagonal elements of the matrix : %d\n", sum);
return 0;
}