Sum of each Row and Column of a Matrix in Java language
ADVERTISEMENTS
Sum of each row and column of a matrix in java language. In this program, you will learn how to find the sum of each row and column of a matrix in java language.
Source Code
// 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