-
Notifications
You must be signed in to change notification settings - Fork 1
/
RowSum_ColumnSum_DiagonalSum.c
55 lines (45 loc) · 1.18 KB
/
RowSum_ColumnSum_DiagonalSum.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
#include <stdio.h>
int main(){
int rows, cols, sumRow, sumCol, sumDia;
printf("Enter the number of Rows and Columns of the Matrix :\n");
scanf("%d %d", &rows, &cols);
int a[rows][cols], i, j;
printf("Enter the Elements of the Matrix :\n");
for(i = 0; i < rows; i++){
for(j = 0; j < cols; j++){
scanf("%d", &a[i][j]);
}
}
printf("\nInput Matrix:");
for(i = 0; i < rows; i++){
printf("\n");
for(j = 0; j < cols; j++){
printf("%d \t", a[i][j]);
}
}
printf("\n\n");
//Calculates sum of each row of the given matrix
for(int i = 0; i < rows; i++){
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
printf("Sum of Row %d : %d\n", (i+1), sumRow);
}
printf("\n");
//Calculates sum of each column of the given matrix
for(int i = 0; i < cols; i++){
sumCol = 0;
for(int j = 0; j < rows; j++){
sumCol = sumCol + a[j][i];
}
printf("Sum of Column %d : %d\n", (i+1), sumCol);
}
printf("\n");
//Calculates sum of the diagonal of the given matrix
for(rows = 0; rows < i; rows++){
sumDia = sumDia + a[rows][rows];
}
printf("Sum of Diagonal : %d", sumDia);
return 0;
}