w3resource

C Exercises: Find the sum of rows an columns of a Matrix

C Array: Exercise-25 with Solution

Write a program in C to find the sum of rows and columns of a matrix.

Pictorial Presentation:

C Exercises: Find the sum of rows an columns of a Matrix

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
     int i,j,k,arr1[10][10],rsum[10],csum[10],n;
	 
       printf("\n\nFind the sum of rows an columns of a Matrix:\n");
       printf("-------------------------------------------\n");	 
	 

     printf("Input the size of the square matrix : ");
     scanf("%d", &n); 
	 printf("Input elements in the first matrix :\n");
       for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&arr1[i][j]);
            }
        }  
	 printf("The matrix is :\n");
	 for(i=0;i<n;i++)
	 {
	   for(j=0;j<n ;j++)
	     printf("% 4d",arr1[i][j]);
	    printf("\n");
	 }

     /* Sum of rows */
     for(i=0;i<n;i++)
     {
	  rsum[i]=0;
	  for(j=0;j<n;j++)
	  rsum[i]=rsum[i]+arr1[i][j];
     }
 
      /* Sum of Column */
      for(i=0;i<n;i++)
      {
	  csum[i]=0;
	  for(j=0;j<n;j++)
		csum[i]=csum[i]+arr1[j][i];
      }
 
      printf("The sum or rows and columns of the matrix is :\n");
      for(i=0;i<n;i++)
      {
	   for(j=0;j<n;j++)
	      printf("% 4d",arr1[i][j]);
	   printf("% 8d",rsum[i]);
	   printf("\n");
       }
       printf("\n");
	    for(j=0;j<n;j++)
             {
	        printf("% 4d",csum[j]);
             }
            printf("\n\n"); 
  }

Sample Output:

Find the sum of rows an columns of a Matrix:                                                                  
-------------------------------------------                                                                   
Input the size of the square matrix : 2                                                                       
Input elements in the first matrix :                                                                          
element - [0],[0] : 5                                                                                         
element - [0],[1] : 6                                                                                         
element - [1],[0] : 7                                                                                         
element - [1],[1] : 8                                                                                         
The matrix is :                                                                                               
   5   6                                                                                                      
   7   8                                                                                                      
The sum or rows and columns of the matrix is :                                                                
   5   6      11                                                                                              
   7   8      15                                                                                              
                                                                                                              
  12  14 

Flowchart:

Flowchart: Find the sum of rows an columns of a Matrix.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to find sum of left diagonals of a matrix.
Next: Write a program in C to print or display the lower triangular of a given matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP