# C Program to find sum of each row in a Matrix

Write a C Program to find the Sum of each row in a Matrix. Or How to write a C program to find Sum of rows in a Multi-Dimensional Array with example.

## C Program to find sum of each row in a Matrix Example 1

This program allows you to enter the total number of rows and columns in a Matrix. Next, we are going to calculate the sum of matrix rows using For Loop.

```/* C Program to find Sum of rows in a Matrix  */

#include<stdio.h>

int main()
{
int i, j, rows, columns, a[10][10], Sum;

printf("Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

printf("Please Enter the Matrix Row and Column Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}

for(rows = 0; rows < i; rows++)
{
Sum = 0;
for(columns = 0; columns < j; columns++)
{
Sum = Sum + a[rows][columns];
}
printf("The Sum of Elements of a Rows in a Matrix =  %d \n", Sum );
}

return 0;
}```

In this C Program to find sum of each row in a Matrix, we declared a Two-dimensional array of the size of 10 * 10. Next, printf statement ask user to enter the Matrix size (rows & columns. For instance 3 Rows, 3 Columns = a[3][3] )

Next, we used for loop to iterate each cell present in a[3][3] Matrix. scanf statement inside the for loop will store the user entered values in every individual array element such as a[0][0], a[0][1], …..

In this C Program to find sum of each row in a Matrix demo, User inserted values are: a[3][3] = {{10, 20, 30}, { 12, 22, 32}, {44, 55, 121}}

Row First Iteration: for(rows = 0; rows < 3; 0++)
The condition (0 < 3) is True.
Sum = 0

Column First Iteration: for(columns = 0; 0 < 3; 0++)
The condition (0 < 3) is True. So, it will start executing the statements inside the loop
Sum  = Sum + a[rows][columns]
Sum  = Sum + a[0][0] => 0 + 10 = 10

Column Second Iteration: for(columns = 1; 1 < 3; 1++) – Condition True
Sum  = Sum + a[0][1] => 10 + 20 = 30

Column Third Iteration: for(columns = 2; 2 < 3; 2++) – Condition True
Sum  = Sum + a[0][2] => 30 + 30 = 60

Next, the column’s value will increment to 4. Condition (columns < 3) will fail. So it will exit from the loop.

Next, we used the C Programming Printf statement to print the Sum. After this, rows value incremented to 1, and Sum will become 0.

Repeat the above steps for rows = 1 and rows = 2

## Find sum of each row in a Matrix Example 2

This C program is the same as the above, but this time we separated the code using functions.

```/* C Program to find Sum of rows in a Matrix  */

#include<stdio.h>

void addRows(int arr[10][10], int i, int j)
{
int rows, columns;
for(rows = 0; rows < i; rows++)
{
int Sum = 0;
for(columns = 0; columns < j; columns++)
{
Sum = Sum + arr[rows][columns];
}
printf("The Sum of Elements of a Rows in a Matrix =  %d \n", Sum );
}
}
int main()
{
int i, j, rows, columns, a[10][10];

printf("Please Enter Number of rows and columns  :  ");
scanf("%d %d", &i, &j);

printf("Please Enter the Matrix Row and Column Elements \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0; columns < j; columns++)
{
scanf("%d", &a[rows][columns]);
}
}

``````Please Enter Number of rows and columns  :  3 3