logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

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

Posted on November 7, 2012 by Manish

This is a program C to find the sum of each row & each column of a MxN Matrix

Problem Description

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.

Problem Solution

1. Take the MxN matrix as input.
2. Use for loops to calculate the sum of the elements of each row & column in a given matrix.

advertisement
Program/Source Code

Here is source code of the C program to find the sum of each row & each column of a MxN matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

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

1. Take M & N of a MxN matrix as input and store it in the variables row & column respectively.
2. Take all the elements of the matrix using two for loops and store in the array a[][].
3. Now to calculate sum of each row and each column, make a nested loop, where first index of matrix will remain constant and second will increment to access each element of the row, adding to get the sum.
4. After this the upper loop increments by 1 to go to the next row.
5. Same function is followed to get sum of all columns except upper loop is provided for tracking columns and lower loop for tracking rows.

advertisement
Runtime Test Cases
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.

« Prev Page - C Program to Compute the Sum of two One-Dimensional Arrays using Malloc
» Next Page - C Program to do the Sum of the Main & Opposite Diagonal Elements of a MxN Matrix

« C Program to Compute the Sum of two One-Dimensional Arrays using Malloc
C Program to do the Sum of the Main & Opposite Diagonal Elements of a MxN Matrix »

Deep Dive @ Sanfoundry:

  1. C Programming Examples on Stacks & Queues
  2. C Programming Examples on Combinatorial Problems & Algorithms
  3. Java Programming Examples on Data-Structures
  4. Java Programming Examples on Collections
  5. C Programming Examples on Data-Structures
  6. C Programming Examples on Arrays
  7. C# Programming Examples on Arrays
  8. Java Programming Examples on Arrays
  9. C# Programming Examples on Matrix
  10. C Programming Examples on Matrix
Manish Bhojasia, a technology veteran with 20+ 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:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.