C Program to Find Sum of Each Row and Column of a Matrix
ADVERTISEMENTS
C program to find sum of each row and column of a matrix. In this article, you will learn how to find sum of each row and column of a matrix.
Source Code
// C Program to Find Sum of Each Row and Column of a Matrix
#include <stdio.h>
int main() {
int a[10][10], i, j, x, y, sum = 0;
printf("-----Enter the number of rows & columns of the matrix-----\n");
scanf("%d %d", &x, &y);
printf("\n-----Enter the co-efficients of the matrix-----\n");
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
scanf("%d", &a[i][j]);
}
printf("\n");
}
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
sum = sum + a[i][j] ;
}
printf("The Sum of the %d position row is = %d\n", i, sum);
sum = 0;
}
printf("\n");
sum = 0;
for (j = 0; j < y; ++j) {
for (i = 0; i < x; ++i) {
sum = sum + a[i][j];
}
printf("The Sum of the %d position column is = %d\n", j, sum);
sum = 0;
}
return 0;
}
Output
-----Enter the number of rows & columns of the matrix-----
3
4
-----Enter the co-efficients of the matrix-----
43
54
2
56
87
4
6
89
32
54
67
1
The Sum of the 0 position row is = 155
The Sum of the 1 position row is = 186
The Sum of the 2 position row is = 154
The Sum of the 0 position column is = 162
The Sum of the 1 position column is = 112
The Sum of the 2 position column is = 75
The Sum of the 3 position column is = 146