Write C Program to Find sum of each row and columns of a matrix


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


  1. #include <stdio.h>
  2.  
  3. #define size 3 // Defining Matrix size
  4.  
  5. int main()
  6. {
  7. int Arr[size][size];
  8. int row, col, sum = 0;
  9.  
  10. //Inputing elements in matrix from user
  11. printf("Enter elements in matrix of size %dx%d: \n", size, size);
  12. for(row=0; row<size; row++)
  13. {
  14. for(col=0; col<size; col++)
  15. {
  16. scanf("%d", &Arr[row][col]);
  17. }
  18. }
  19.  
  20. // Calculating sum of elements of each row of matrix
  21. for(row=0; row<size; row++)
  22. {
  23. sum = 0;
  24. for(col=0; col<size; col++)
  25. {
  26. sum += Arr[row][col];
  27. }
  28. //Printing Sum of row elements
  29. printf("Sum of elements of row %d = %d\n", row+1, sum);
  30. }
  31.  
  32. // Finding sum of elements of each columns of matrix
  33. for(row=0; row<size; row++)
  34. {
  35. sum = 0;
  36. for(col=0; col<size; col++)
  37. {
  38. sum += Arr[col][row];
  39. }
  40. //Printing Sum of columns elements
  41. printf("Sum of elements of column %d = %d\n", row+1, sum);
  42. }
  43.  
  44. return 0;
  45. }
  46.  

Result

C language techstudy.org

     






Related Topics




Popular Posts