Java Online Compiler
Example: Sum of each Row and Column of a Matrix in Java language
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// 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; } } }
3 4 12 43 55 23 56 34 76 23 65 23 45 23
Output
Clear
ADVERTISEMENTS