This is a program C to find the sum of each row & each column of a MxN Matrix
This C Program finds the sum of each row & each column of a MxN matrix. The program accepts an MxN matrix. Then adds each row of the matrix and also adds each column of the matrix.
1. Take the MxN matrix as input.
2. Use for loops to calculate the sum of the elements of each row & column in a given matrix.
Here is source code of the C program to find the sum of each row & each column of a MxN matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C program to accept a matrix of order M x N and find the sum
* of each row and each column of a matrix
*/
#include <stdio.h>
void main ()
{
static int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
1. Take M & N of a MxN matrix as input and store it in the variables row & column respectively.
2. Take all the elements of the matrix using two for loops and store in the array a[][].
3. Now to calculate sum of each row and each column, make a nested loop, where first index of matrix will remain constant and second will increment to access each element of the row, adding to get the sum.
4. After this the upper loop increments by 1 to go to the next row.
5. Same function is followed to get sum of all columns except upper loop is provided for tracking columns and lower loop for tracking rows.
Enter the order of the matrix 2 2 Enter the co-efficients of the matrix 23 45 80 97 Sum of the 0 row is = 68 Sum of the 1 row is = 177 Sum of the 0 column is = 103 Sum of the 1 column is = 142
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Practice Computer Science MCQs
- Apply for Computer Science Internship
- Check Computer Science Books
- Apply for C Internship
- Check C Books