C Program to Find the Sum of each Row & each Column of a MxN Matrix

This C Program finds the sum of each row & each column of a MxN matrix. The program accepts an MxN matrix. Then adds each row of the matrix and also adds each column of the matrix.

Here is source code of the C program to find the sum of each row & each column of a MxN matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to accept a matrix of order M x N and find the sum
  3.  * of each row and each column of a matrix
  4.  */
  5. #include <stdio.h>
  6.  
  7. void main ()
  8. {
  9.     static int array[10][10];
  10.     int i, j, m, n, sum = 0;
  11.  
  12.     printf("Enter the order of the matrix\n");
  13.     scanf("%d %d", &m, &n);
  14.     printf("Enter the co-efficients of the matrix\n");
  15.     for (i = 0; i < m; ++i)
  16.     {
  17.         for (j = 0; j < n; ++j)
  18.         {
  19.             scanf("%d", &array[i][j]);
  20.         }
  21.     }
  22.     for (i = 0; i < m; ++i)
  23.     {
  24.         for (j = 0; j < n; ++j)
  25.         {
  26.             sum = sum + array[i][j] ;
  27.         }
  28.         printf("Sum of the %d row is = %d\n", i, sum);
  29.         sum = 0;
  30.     }
  31.     sum = 0;
  32.     for (j = 0; j < n; ++j)
  33.     {
  34.         for (i = 0; i < m; ++i)
  35.         {
  36.             sum = sum + array[i][j];
  37.         }
  38.         printf("Sum of the %d column is = %d\n", j, sum);
  39.         sum = 0;
  40.     }
  41. }


$ cc pgm82.c
$ a.out
Enter the order of the matrix
2 2
Enter the co-efficients of the matrix
23 45
80 97
Sum of the 0 row is = 68
Sum of the 1 row is = 177
Sum of the 0 column is = 103
Sum of the 1 column is = 142

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Matrix, go to C Programming Examples on Matrix. If you wish to look at programming examples on all topics, go to C Programming Examples.
Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Follow Manish & Sanfoundry