-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculatesumofrowcolumn.c
More file actions
39 lines (32 loc) · 950 Bytes
/
calculatesumofrowcolumn.c
File metadata and controls
39 lines (32 loc) · 950 Bytes
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
#include <stdio.h>
int main() {
int rows, cols, i, j;
// Input number of rows and columns
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int matrix[rows][cols];
// Input elements of the matrix
printf("Enter the elements of the matrix:\n");
for(i = 0; i < rows; ++i) {
for(j = 0; j < cols; ++j) {
scanf("%d", &matrix[i][j]);
}
}
// Calculate the sum of each row
for(i = 0; i < rows; ++i) {
int rowSum = 0;
for(j = 0; j < cols; ++j) {
rowSum += matrix[i][j];
}
printf("Sum of row %d = %d\n", i + 1, rowSum);
}
// Calculate the sum of each column
for(j = 0; j < cols; ++j) {
int colSum = 0;
for(i = 0; i < rows; ++i) {
colSum += matrix[i][j];
}
printf("Sum of column %d = %d\n", j + 1, colSum);
}
return 0;
}