Javatpoint Logo
Javatpoint Logo

Program to find the sum of each row and each column of a matrix

Explanation

In this program, we need to calculate the sum of elements in each row and each column of the given matrix.

Program to find the sum of each row and each column of a matrix

Above diagram shows the sum of elements of each row and each column of a matrix.

Algorithm

  1. Declare and initialize a two-dimensional array a.
  2. Calculate the number of rows and columns present in the array a and store it in variables rows and cols respectively.
  3. Maintain two variables sumRow and sumCol to store the sum of elements in the specific row and the sum of elements in specific column respectively.
  4. To calculate the sum of elements in each row:
    1. Two loops will be used to traverse the array where the outer loop selects a row, and the inner loop represents the columns present in the matrix a.
    2. Calculate the sum by adding elements present in a row.
    3. Display sumRow.
    4. Repeat this for each row.
  5. To calculate the sum of elements in each column:
    1. Two loops will be used to traverse the array where the outer loop select a column, and the inner loop represents the rows present in the matrix a.
    2. Calculate the sum by adding elements present in a column.
    3. Display sumCol.
    4. Repeat this for each column.

Solution

Python

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

C

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

JAVA

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

C#

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

PHP

Output:

Sum of 1 row: 6
Sum of 2 row: 15
Sum of 3 row: 24
Sum of 1 column: 12
Sum of 2 column: 15
Sum of 3 column: 18

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA