# How to find the sum of each row and column of a matrix in java language

How to find the sum of each row and column of a matrix in __java language__. There are you will learn how to find the sum of each row & column of a matrix.

Take an example to find out this solution through a __java program__:

```
// How to find the sum of each row and
// column of a matrix in java language
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a[][], i, j, x, y, sum = 0;
a = new int[10][10];
System.out.print("-----Enter the number of rows & columns of the matrix-----\n");
x = in.nextInt();
y = in.nextInt();
System.out.print("\n-----Enter the co-efficients of the matrix-----\n");
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
a[i][j] = in.nextInt();
}
System.out.print("\n");
}
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
sum = sum + a[i][j] ;
}
System.out.print("The Sum of the " + i + " position row is = " + sum + "\n");
sum = 0;
}
System.out.print("\n");
sum = 0;
for (j = 0; j < y; ++j) {
for (i = 0; i < x; ++i) {
sum = sum + a[i][j];
}
System.out.print("The Sum of the " + j + " position column is = " + sum + "\n");
sum = 0;
}
}
}
```

**Output:**

`-----Enter the number of rows & columns of the matrix-----`

3

4

`-----Enter the co-efficients of the matrix-----`

12

43

55

23

`56`

34

76

23

`65`

23

45

23

`The Sum of the 0 position row is = 133`

The Sum of the 1 position row is = 189

The Sum of the 2 position row is = 156

`The Sum of the 0 position column is = 133`

The Sum of the 1 position column is = 100

The Sum of the 2 position column is = 176

The Sum of the 3 position column is = 69

## Tags:

# write a program to find the sum of all elements in a matrix in java

# write a java program to calculate the sum of all even elements of a matrix

# write a program to find the sum of matrix elements in java using functions