Row and Column wise Sum in Array

In this class, We discuss Row and Column wise Sum in Array.

The reader should have prior knowledge of two-dimensional arrays. Click Here.

Question:

Write a Java program to display the sum of elements of each row and column.

Example:

The below diagram shows the two-dimensional array.

The sum of elements in the first line is 10.

The sum of elements in the second line is 26.

We need to display the sum for all the lines and columns.

Code:

The below diagram shows the code.

We use a nested loop to access elements row-wise.

The outer loop takes the line indexes.

For each line index, the inner loop takes the column indexes to read the entire line.

The sum value will be displayed after coming out of the inner loop.

After displaying the sum value, next time, we make sum =0.

Similarly, we use a nested loop to display each column sum.

The outer loop takes the column index, and the inner loop takes the row index.