-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06sept_1.c
41 lines (38 loc) · 938 Bytes
/
06sept_1.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
// : Write a program to print the sum of a 2D matrice/array and separately print the results of rows and columns
#include <stdio.h>
int main()
{
int rows, cols, i, j, sum = 0;
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
int matrix[rows][cols];
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < rows; i++)
{
sum = 0;
for (j = 0; j < cols; j++)
{
sum += matrix[i][j];
}
printf("Sum of row %d: %d\n", i, sum);
}
for (j = 0; j < cols; j++)
{
sum = 0;
for (i = 0; i < rows; i++)
{
sum += matrix[i][j];
}
printf("Sum of column %d: %d\n", j, sum);
}
return 0;
}