# Java Program to find Sum of each Matrix Row

Write a Java Program to Find the Sum of each Matrix Row with an example. Or Java Program to calculate the sum of each and every row in a given Matrix or multi-dimensional array.

In this Java Matrix row sum example, we declared a 3 * 3 SumOfRows_arr integer matrix with random values. Next, we used for loop to iterate the SumOfRows_arr Matrix items. Within the for loop, we are calculating the sum of SumOfRows_arr Matrix rows.

```public class SumOfMatrixRows {

public static void main(String[] args) {

int i, j, sum;

int[][] SumOfRows_arr = {{15, 25, 35}, {45, 55, 65}, {75, 85, 95}};

for(i = 0; i < SumOfRows_arr.length; i++)
{
sum = 0;
for(j = 0; j < SumOfRows_arr.length; j++)
{
sum = sum + SumOfRows_arr[i][j];
}
System.out.println("\nThe Sum of Matrix Items in  " + i + " row = " + sum);
}
}
}```

Java sum of matrix rows output

``````
The Sum of Matrix Items in  0 row = 75

The Sum of Matrix Items in  1 row = 165

The Sum of Matrix Items in  2 row = 255``````

## Java Program to find Sum of each Matrix Row example 2

This Java Matrix sum of each row code is the same as the above. However, this Java code for sum of matrix row allows the user to enter the number of rows, columns, and the matrix items. Please refer to C Program to find Sum of each Row in a Matrix article to understand the for loop execution in iteration wise.

```import java.util.Scanner;

public class SumOfMatrixRows {

private static Scanner sc;

public static void main(String[] args) {

int i, j, rows, columns, sum = 0;

sc= new Scanner(System.in);

System.out.println("\n Enter Matrix Rows and Columns :  ");
rows = sc.nextInt();
columns = sc.nextInt();

int[][] SumOfRows_arr = new int[rows][columns];

System.out.println("\n Please Enter the Matrix Items :  ");
for(i = 0; i < rows; i++) {
for(j = 0; j < columns; j++) {
SumOfRows_arr[i][j] = sc.nextInt();
}
}

for(i = 0; i < SumOfRows_arr.length; i++)
{
sum = 0;
for(j = 0; j < SumOfRows_arr.length; j++)
{
sum = sum + SumOfRows_arr[i][j];
}
System.out.println("\nThe Sum of Matrix Items in  " + i + " row = " + sum);
}
}
}```