﻿ C exercises: Find the sum of row an column of a Matrix - w3resource

# C Exercises: Find the sum of rows an columns of a Matrix

## C Array: Exercise-25 with Solution

Write a program in C to find the sum of rows an columns of a Matrix.

Pictorial Presentation: Sample Solution:

C Code:

``````#include <stdio.h>

void main()
{
int i,j,k,arr1,rsum,csum,n;

printf("\n\nFind the sum of rows an columns of a Matrix:\n");
printf("-------------------------------------------\n");

printf("Input the size of the square matrix : ");
scanf("%d", &n);
printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("The matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++)
printf("% 4d",arr1[i][j]);
printf("\n");
}

/* Sum of rows */
for(i=0;i<n;i++)
{
rsum[i]=0;
for(j=0;j<n;j++)
rsum[i]=rsum[i]+arr1[i][j];
}

/* Sum of Column */
for(i=0;i<n;i++)
{
csum[i]=0;
for(j=0;j<n;j++)
csum[i]=csum[i]+arr1[j][i];
}

printf("The sum or rows and columns of the matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("% 4d",arr1[i][j]);
printf("% 8d",rsum[i]);
printf("\n");
}
printf("\n");
for(j=0;j<n;j++)
{
printf("% 4d",csum[j]);
}
printf("\n\n");
}
```
```

Sample Output:

```Find the sum of rows an columns of a Matrix:
-------------------------------------------
Input the size of the square matrix : 2
Input elements in the first matrix :
element - , : 5
element - , : 6
element - , : 7
element - , : 8
The matrix is :
5   6
7   8
The sum or rows and columns of the matrix is :
5   6      11
7   8      15

12  14
```

Flowchart: C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

﻿

Inviting useful, relevant, well-written and unique guest posts