September 22, 2021, Learn eTutorial

644

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

Here in this C program, we need to add the rows and columns of a matrix separately, which means if we have a matrix with three rows and three columns, we need to have six separate results. Example: If we have a matrix A

**2 5 **

**10 2**

Then the sum of the first row=2+5=7

The sum of the second row=10+2=12

Sum of the First column=2+10=12

Sum of the second column=5+2=7

so here in this C program, we accept a matrix from the user and just display the matrix as such. Now we need to declare a variable called sum and initialize it to zero. now we open a nested `for loop`

and we increment the row elements in the inner `for loop`

to get the sum of rows separately.

Similarly, we open the next `for loop`

for the column sum and do the same as we have done in the row, but in this, we increment the column instead of the row. Finally, we display each sum as separately.

STEP 1:Include the header files to use the built-in header files into the C program.

STEP 2: Declare the variable** i,j,m,n,sum=0,m1[][]**.

STEP 3:Read the order of the matrix into the variable **m** and **n**.

STEP 4:Read the co-efficient of the matrix into the array **m1[i][j]** using nested for loop.

STEP 5:By using for loop calculate the row sum as** sum=sum+m1[i][j]**.And display the sum of row **i **as a **sum**.

STEP 6: Set **sum=0** and repeat step 5 until** i**

STEP 7: By using nested for loop calculate coulumn **sum **as **sum=sum+m1[i][j]** and display it.

STEP 1:Include the header files to use the built-in header files into the C program.

STEP 2: Declare the variable **i,j,m,n,sum=0,m1[][]**.

STEP 3:Read the order of the matrix into the variable **m **and **n**.

STEP 4:Read the co-efficient of the matrix into the array **m1[i][j]** using nested for loop.

STEP 5:By using `for loop`

calculate the row sum as **sum=sum+m1[i][j]**.And display the sum of row** i **as a sum.

` ````
#include <stdio.h>
void main() {
static int m1[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n"); /* accepting the order of the matrix */
scanf("%d %d", & m, & n);
printf("Enter the co-efficient of the matrix\n"); /* enters the elements of matrix */
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
scanf("%d", & m1[i][j]);
}
}
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
sum = sum + m1[i][j]; /* calculating the row sum separately for each row */
}
printf("The Sum of the %d rows is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j) {
for (i = 0; i < m; ++i) {
sum = sum + m1[i][j]; /* calculating the column sum separately as we done in row */
}
printf("The Sum of the %d columns is = %d\n", j, sum);
sum = 0;
}
} /*End of main() */
```

Enter the order of the matrix 3*3 Enter the co-efficient s of the matrix 1 2 3 4 5 6 7 8 9 The Sum of the 0 rows is = 6 The Sum of the 1 row is = 15 The Sum of the 2 rows is = 24 The Sum of the 0 columns is = 12 The Sum of the 1 column is = 15 The Sum of the 2 columns is = 18