by Dinesh Thakur

This is C Program to calculate the sum of the elements of each row & column. A matrix is made of rows and columns. The program will add all the rows and the columns of the matrix.

In this program a composed matrix will be used to add the rows and columns. The required variables are also declared that are contain the value. Array type variables also use for the loop statement. In this program nested for loop used to computing the sum for rows and columns of a composed matrix. First loop will be add to make matrix the other one will be the computing the rows addition and the next will be do the same for columns. In the last instance the result will be displayed on the screen.
Problem Statement :
This program will evaluate the sum of each row and column from composed matrix.

  1. Declaring array type variables.
  2. Using loop statement as required.
  3. Display the result on the screen.

Here is source code of the C program to compute the rows and columns of composed matrix. The C program is successfully compiled. The program output is also shown below.


#include<stdio.h>
void main()
{
      int a[10][10],i,j,m,n;
      clrscr();
      printf("\nEnter Order of Matrix : ");
      scanf("%d%d",&m,&n);
      printf("\nEnter Elements : ");
      for(i=0;i<m;i++)
           for(j=0;j<n;j++)
                scanf("%d",&a[i][j]);
                for(i=0;i<m;i++)
                    {
                          a[i][n]=0;
                          for(j=0;j<n;j++)
                               a[i][n]+=a[i][j];
                     }
                          for(j=0;j<n;j++)
                              {
                                 a[m][j]=0;
                                 for(i=0;i<m;i++)
                                      a[m][j]+=a[i][j];
                               }
                                 a[m][n]=0;
                                 for(i=0;i<m;i++)
                                       a[m][n]+=a[i][n];
                                       for(i=0;i<m+1;i++)
                                           {
                                              for(j=0;j<n+1;j++)
                                                  {
                                                       printf("%d",a[i][j]);
                                                       printf(" ");
                                                   }
                                                       printf("\n");
                                            }
                                                       getch();
}

Sum of each Row and each Col of a Matrix