# C Program to find the sum of elements of each row and column

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

## How to add the rows and columns of a matrix?

Here in this C program, we need to Calculate the Sum of each Row and Column of a Matrix separately using Functions. We are doing is to make two Functions individually, One for the Row Addition and one for Column Addition. And we call the respective Function to do the respective Operation.

Then we pass the Parameters into the Function such as an Array, Row name, and Column name depends on the Function we called. In that Function, we calculate the Sum of each Row or Column separately using the formula. We pass the result into the main C program, and we display the result in the main program.

The main logic of this program is to accept a Matrix from the user and store it into an array arr[10][10]. Then display the Matrix using For loop. We call the respective Function and do all the processes in the Function and return the result to the main program.

### ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the Array arr[10][10] and the integer variable Row and Col.

STEP 3: Declare two functions int Addrow(int A[10][10], int k, int c) and int Addcol(int A[10][10], int k, int c).

STEP 4: Accept the Order of the Matrix from the user into row and col.

STEP 5: Accept the Matrix from the user by using nested For loop and store it into arr[10][10].

STEP 6: Print the Matrix arr[10][10] using For loop.

STEP 7: Initialize i=0

STEP 8: Check i

STEP 10: Print Sum of Row i+1 as rowsum.Increment 'i' by one and repeat step 8.

STEP 11: Initialize j=0

STEP 12: Check j

STEP 14: Print Sum of Column as colsum.Increment 'j' by one and repeat step 12.

STEP 15: Initialize j=0

STEP 16: Check j

STEP 17: Calculate sumall=sumall + Addrow(arr,j,col)

STEP 18: Print the Sum of all elements of the Matrix as sumall.

### `Function int Addrow(int A[10][10], int k, int c)`

STEP 1: Declare the variable rsum=0;

STEP 2: Using For loop find out rsum = rsum + A[k][i].

STEP 3: Return rsum.

### `Function int Addcol(int A[10][10], int k, int r)   `

STEP 1: Declare the variables csum=0.

STEP 2: Using for loop find out csum = csum + A[j][k];

STEP 3: Return csum.

## C Source Code

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

void main() {
int arr[10][10];
int i, j, row, col, rowsum, colsum, sumall = 0;
int Addrow(int A[10][10], int k, int c); /* Function declaration */
int Addcol(int A[10][10], int k, int c);
printf("Enter the order of the matrix\n");
scanf("%d %d", & row, & col);
printf("Enter the elements of the matrix\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
scanf("%d", & arr[i][j]);
}
}
printf("Input matrix is\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
printf("%d", arr[i][j]);
}
printf("\n");
}
for (i = 0; i < row; i++) /* computing row sum */ {
printf("Sum of row %d = %d\n", i + 1, rowsum);
}
for (j = 0; j < col; j++) {
printf("Sum of column  %d = %d\n", j + 1, colsum);
}
for (j = 0; j < row; j++) /* computation of all elements */ {
sumall = sumall + Addrow(arr, j, col);
}
printf("Sum of all elements of matrix = %d\n", sumall);
}
int Addrow(int A[10][10], int k, int c) /* Function to add each row */ {
int rsum = 0, i;
for (i = 0; i < c; i++) {
rsum = rsum + A[k][i];
}
return (rsum);
}
int Addcol(int A[10][10], int k, int r) /* Function to add each column */ {
int csum = 0, j;
for (j = 0; j < r; j++) {
csum = csum + A[j][k];
}
return (csum);
}```
```

## OUTPUT

```Enter the order of the matrix
3*3

Enter the elements of the matrix
1 2 3
4 5 6
7 8 9

Input matrix is
1  2  3
4  5  6
7  8  9

Sum of row 1 = 6

Sum of row 2 = 15

Sum of row 3 = 24

Sum of column  1 = 12

Sum of column  2 = 15

Sum of column  3 = 18

Sum of all elements of matrix = 45```
VIEW ALL
VIEW ALL
VIEW ALL