C Program to find Sum of each row and column of a Matrix

How to write a C Program to find Sum of each row and column of a Matrix?. Or, Write a C program to find Sum of each row and column of a Multi-Dimensional Array with example.

C Program to find Sum of each row and column of a Matrix 1

C Program to find Sum of each row and column of a Matrix Example 1

This program allows the user to enter the number of rows and columns of a Matrix. Next, we are going to calculate the sum of each row and column elements in this matrix using For Loop.

/* C Program to find Sum of each row and column of a Matrix */

#include<stdio.h>
 
int main()
{
 	int i, j, rows, columns, a[10][10], Sum;
  
 	printf("\n Please Enter Number of rows and columns  :  ");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the Matrix 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("\n The Sum of Elements of a Rows in a Matrix =  %d", Sum );
  	}

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

 	return 0;
}
C Program to find Sum of each row and column of a Matrix 2

In this C Program to find Sum of each row and column of a Matrix, We declared single Two dimensional arrays Multiplication of size of 10 * 10.
Below statements asks the User to enter the Matrix size (Number of rows and columns. For instance 2 Rows, 3 Columns = a[2][3] )

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

Next, we used C Programming for loop to iterate each cell present in a[3][3] matrix. Conditions inside the for loops ((rows < i) and (columns < j)) will ensure the compiler, not to exceed the matrix limit. Otherwise, the matrix will overflow

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], …..

for(rows = 0; rows < i; rows++).
{
  for(columns = 0; columns < j; columns++)
   {
     scanf("%d", &a[rows][columns]);
   }
}

In the next line, We have one more for loop to find Sum of Row Elements of a Matrix

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

User inserted values for C Program to find Sum of each row and column of a Matrix are: a[3][3] = {{10, 20, 30}, { 40, 50, 60}, {70, 80, 90}}

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 (columns < 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++)
The condition (columns < 3) is True.
Sum  = Sum + a[0][1] => 10 + 20 = 30

Column Third Iteration: for(columns = 2; 2 < 3; 2++)
The condition (columns < 3) is True.
Sum  = Sum + a[0][2] => 30 + 30 = 60

Next, the column’s value will increment. After the increment, the condition inside the second for loop (columns < 3) will fail. So it will exit from the loop.

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

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

In the next line, We have one more for loop to find Sum of Column Elements of a Matrix

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

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 (columns < 3) is True. So, it will start executing the statements inside the loop
Sum  = Sum + a[columns][rows]
Sum  = Sum + a[0][0] => 0 + 10 = 10

Column Second Iteration: for(columns = 1; 1 < 3; 1++)
The condition (columns < 3) is True.
Sum  = Sum + a[1][0] => 10 + 40 = 50

Column Third Iteration: for(columns = 2; 2 < 3; 2++)
The condition (columns < 3) is True.
Sum  = Sum + a[2][0] => 50 + 70 = 120

Next, columns value will be incremented. After the increment the condition inside the second for loop (columns < 3) will fail. So it will exit from the loop.

Next, we used the 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

C Program to find Sum of each row and column of a Matrix Example 2

This C program for sum of matrix rows and columns is the same as above, but this time we organized the code using two different functions.

/* C Program to find Sum of each row and column of a Matrix */

#include<stdio.h>

void AddRows(int arr[10][10], int i, int j);
void AddColumns(int arr[10][10], int i, int j);
 
int main()
{
 	int i, j, rows, columns, a[10][10], RowSum, ColumnSum;
  
 	printf("\n Please Enter Number of rows and columns  :  ");
 	scanf("%d %d", &i, &j);
 
 	printf("\n Please Enter the Matrix Elements \n");
 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
      		scanf("%d", &a[rows][columns]);
    	}
  	}

   	AddRows(a, i, j); 
	AddColumns(a, i, j); 	

 	return 0;
} 

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

void AddColumns(int arr[10][10], int i, int j)
{
	int rows, columns, Sum = 0;		
 	for(columns = 0; columns < j; columns++)
  	{
  		for(rows = 0; rows < i; rows++)
  		{
  			Sum = Sum + arr[rows][columns];  			
		}
		printf("\n The Sum of Elements of a Columns in a Matrix =  %d", Sum );
    }
}
 Please Enter Number of rows and columns  :  3 3

 Please Enter the Matrix Elements 
25 35 45
44 75 95
125 86 95

 The Sum of Elements of a Rows in a Matrix =  105
 The Sum of Elements of a Rows in a Matrix =  319
 The Sum of Elements of a Rows in a Matrix =  625
 The Sum of Elements of a Columns in a Matrix =  194
 The Sum of Elements of a Columns in a Matrix =  390
 The Sum of Elements of a Columns in a Matrix =  625