## Introduction

In this article, we will discuss a C Program to find the sum of each row and column of a matrix. To reach this goal, we need to have information about arrays in C.

An array which is of the form 'm * n' is known as a 2-Dimension array or 2-D array. It is also called a matrix. In this case, m is taken for the number of rows, and n is taken for the number of columns.

Here, we are given a 2-Dimensional array, and our task is to find the sum of each row and each column.

Also see: __C Static Function____ __and __Tribonacci Series____.__

### Problem Statement

The problem statement is that Here, we are given a 2-Dimensional array, and our task is to find the sum of each row and each column..

Sample Input

3 3 1 2 3 4 5 6 7 8 9 |

Sample Output:

Calculation of Row Sum Sum of the elements in row 0 is 6. Sum of the elements in row 1 is 15. Sum of the elements in row 2 is 24. Calculation of Column Sum The sum of all the elements in column 0 is 12. The sum of all the elements in column 1 is 15. The sum of all the elements in column 2 is 18. |

Explanation:

There are two ways to calculate the sum of each row and column of a matrix:

Recommended topics, __Binary to Hex Converter__ and __Short int in C Programming__

## Method 1

In this C program, firstly, we will declare the M*N matrix and then find the sum of each row and column, and then we will display the result.

### Algorithm

- Declaration of a 2-D array, i.e., an m * n matrix.
- Initialization of the array using two 'for' loops.
- Declaration of two variables that will store the sum of each row and column.
- Now to calculate the sum of the row, we will use a nested loop.
- Keeping the first index of the matrix constant and incrementing the second index to access each element of the row.
- Keep on the addition of these elements and display the result after coming out of the inner loop.
- Now calculate the sum of the column again using the nested loop.
- Now this time, we will increment the first index of the matrix and keep the second index of the matrix constant to access each element of the column.
- Keep on adding these elements and display the result after coming out of the nested loop.

Implementation

```
#include <stdio.h>
int main()
{
// 'm' is taken for the number of rows, and 'n' is taken for the number of columns
int m, n;
// initialisation of rows and columns by taking input from the user.
printf( " Enter the required number of rows and columns: \n " );
scanf( "%d %d" , &m, &n );
// initialisation of matrix
int arr[m][n];
printf( "Enter the elements of the given matrix: \n" ) ;
for( int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
scanf( "%d", &arr[i][j] );
}
}
printf( "\nThe elements in the matrix are \n" );
for( int i=0; i<m; i++ )
{
for(int j=0; j<n; j++)
{
printf( "%d ", arr[i][j] );
}
printf( "\n" );
}
printf( "\n Calculation of Row Sum \n" );
for(int i=0; i<m; i++)
{
int rowsum=0;
for(int j=0; j<n; j++)
{
rowsum = rowsum + arr[i][j] ;
}
printf( " \n Sum of the elements in row %d is %d \n ", i, rowsum);
}
printf( "\n Calculation of Column Sum \n " );
for( int i=0; i<m; i++ )
{
int colsum=0;
for( int j=0; j<n; j++ )
{
colsum = colsum+arr[j][i] ;
}
printf( " \n The sum of all the elements in column %d is %d\n ", i, colsum );
}
return 0;
}
```

Output:

### Complexity Analysis

**Time Complexity: **O(n^{2})

**Space Complexity: **Constant space.

You can practice by yourself with the help of __online c compiler__.

You can also read about __dynamic array in c__.