Given a matrix of order m*n, the task is to find out the sum of each row and each column of a matrix.
Examples:
Input: array[4][4] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
Output: Sum of the 0 row is = 4
Sum of the 1 row is = 8
Sum of the 2 row is = 12
Sum of the 3 row is = 16
Sum of the 0 column is = 10
Sum of the 1 column is = 10
Sum of the 2 column is = 10
Sum of the 3 column is = 10
Approach:

The sum of each row and each column can be calculated by traversing through the matrix and adding up the elements.
Below is the implementation of the above approach:
C++
// C++ program to find the sum // of each row and column of a matrix #include <iostream> using namespace std; // Get the size m and n #define m 4 #define n 4 // Function to calculate sum of each row void row_sum(int arr[m][n]) { int i,j,sum = 0; cout << "\nFinding Sum of each row:\n\n"; // finding the row sum for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { // Add the element sum = sum + arr[i][j]; } // Print the row sum cout << "Sum of the row " << i << " = " << sum << endl; // Reset the sum sum = 0; } } // Function to calculate sum of each column void column_sum(int arr[m][n]) { int i,j,sum = 0; cout << "\nFinding Sum of each column:\n\n"; // finding the column sum for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { // Add the element sum = sum + arr[j][i]; } // Print the column sum cout << "Sum of the column " << i << " = " << sum << endl; // Reset the sum sum = 0; } } // Driver code int main() { int i,j; int arr[m][n]; // Get the matrix elements int x = 1; for (i = 0; i < m; i++) for (j = 0; j < n; j++) arr[i][j] = x++; // Get each row sum row_sum(arr); // Get each column sum column_sum(arr); return 0; } |
Java
// Java program to find the sum // of each row and column of a matrix import java.io.*; class GFG { // Get the size m and n static int m = 4; static int n = 4; // Function to calculate sum of each row static void row_sum(int arr[][]) { int i,j,sum = 0; System.out.print( "\nFinding Sum of each row:\n\n"); // finding the row sum for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { // Add the element sum = sum + arr[i][j]; } // Print the row sum System.out.println( "Sum of the row " + i + " = " + sum); // Reset the sum sum = 0; } } // Function to calculate sum of each column static void column_sum(int arr[][]) { int i,j,sum = 0; System.out.print( "\nFinding Sum of each column:\n\n"); // finding the column sum for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { // Add the element sum = sum + arr[j][i]; } // Print the column sum System.out.println( "Sum of the column " + i + " = " + sum); // Reset the sum sum = 0; } } // Driver code public static void main (String[] args) { int i,j; int [][]arr = new int[m][n]; // Get the matrix elements int x = 1; for (i = 0; i < m; i++) for (j = 0; j < n; j++) arr[i][j] = x++; // Get each row sum row_sum(arr); // Get each column sum column_sum(arr); } } //This code is contributed by inder_verma.. |
Python 3
# Python3 program to find the sum # of each row and column of a matrix # import numpy library as np alias import numpy as np # Get the size m and n m , n = 4, 4 # Function to calculate sum of each row def row_sum(arr) : sum = 0 print("\nFinding Sum of each row:\n") # finding the row sum for i in range(4) : for j in range(4) : # Add the element sum += arr[i][j] # Print the row sum print("Sum of the row",i,"=",sum) # Reset the sum sum = 0 # Function to calculate sum of each column def column_sum(arr) : sum = 0 print("\nFinding Sum of each column:\n") # finding the column sum for i in range(4) : for j in range(4) : # Add the element sum += arr[j][i] # Print the column sum print("Sum of the column",i,"=",sum) # Reset the sum sum = 0 # Driver code if __name__ == "__main__" : arr = np.zeros((4, 4)) # Get the matrix elements x = 1 for i in range(m) : for j in range(n) : arr[i][j] = x x += 1 # Get each row sum row_sum(arr) # Get each column sum column_sum(arr) # This code is contributed by # ANKITRAI1 |
C#
// C# program to find the sum // of each row and column of a matrix using System; class GFG { // Get the size m and n static int m = 4; static int n = 4; // Function to calculate sum of each row static void row_sum(int[,] arr) { int i,j,sum = 0; Console.Write("\nFinding Sum of each row:\n\n"); // finding the row sum for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { // Add the element sum = sum + arr[i, j]; } // Print the row sum Console.WriteLine( "Sum of the row " + i + " = " + sum); // Reset the sum sum = 0; } } // Function to calculate sum // of each column static void column_sum(int[,] arr) { int i, j, sum = 0; Console.Write("\nFinding Sum of each" + " column:\n\n"); // finding the column sum for (i = 0; i < 4; ++i) { for (j = 0; j < 4; ++j) { // Add the element sum = sum + arr[j, i]; } // Print the column sum Console.WriteLine("Sum of the column " + i + " = " + sum); // Reset the sum sum = 0; } } // Driver code public static void Main () { int i, j; int[,] arr = new int[m, n]; // Get the matrix elements int x = 1; for (i = 0; i < m; i++) for (j = 0; j < n; j++) arr[i, j] = x++; // Get each row sum row_sum(arr); // Get each column sum column_sum(arr); } } // This code is contributed // by Akanksha Rai(Abby_akku) |
PHP
<?php // PHP program to find the sum // of each row and column of a matrix // Get the size m and n $m = 4; $n = 4; // Function to calculate sum of each row function row_sum(&$arr) { $sum = 0; echo "Finding Sum of each row:\n\n"; // finding the row sum for ($i = 0; $i < 4; ++$i) { for ($j = 0; $j < 4; ++$j) { // Add the element $sum = $sum + $arr[$i][$j]; } // Print the row sum echo "Sum of the row " . $i . " = " . $sum . "\n"; // Reset the sum $sum = 0; } } // Function to calculate sum of each column function column_sum(&$arr) { $sum = 0; echo "\nFinding Sum of each column:\n\n"; // finding the column sum for ($i = 0; $i < 4; ++$i) { for ($j = 0; $j < 4; ++$j) { // Add the element $sum = $sum + $arr[$j][$i]; } // Print the column sum echo "Sum of the column " . $i . " = " . $sum . "\n"; // Reset the sum $sum = 0; } } // Driver code $arr = array_fill(0, $m, array_fill(0, $n, NULL)); // Get the matrix elements $x = 1; for ($i = 0; $i < $m; $i++) for ($j = 0; $j < $n; $j++) $arr[$i][$j] = $x++; // Get each row sum row_sum($arr); // Get each column sum column_sum($arr); // This code is contributed by ita_c ?> |
Finding Sum of each row: Sum of the row 0 = 10 Sum of the row 1 = 26 Sum of the row 2 = 42 Sum of the row 3 = 58 Finding Sum of each column: Sum of the column 0 = 28 Sum of the column 1 = 32 Sum of the column 2 = 36 Sum of the column 3 = 40
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum element of each row and each column in a matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Maximum sum of a Matrix where each value is from a unique row and column
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Minimum operations required to make each row and column of matrix equals
- Print an N x M matrix such that each row and column has all the vowels in it
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Find a Square Matrix such that sum of elements in every row and column is K
- Sum of middle row and column in Matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Find the original matrix when largest element in a row and a column are given
- Find if a binary matrix exists with given row and column sums
- Find all matrix elements which are minimum in their row and maximum in their column
- Print all elements in sorted order from row and column wise sorted matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

