Sum of two matrix
unknown
java
2 years ago
958 B
6
Indexable
import java.util.Scanner;
class Sum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
int[][] sumMatrix = new int[rows][columns];
for (int twice = 0; twice < 2; twice++) {
System.out.println("Enter elements of Matrix " + (twice + 1) + ":");
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++)
sumMatrix[i][j] += scanner.nextInt();
}
System.out.println("Sum of the matrices:");
for (int[] row : sumMatrix) {
for (int element : row)
System.out.printf("%-5d", element);
System.out.println();
}
scanner.close();
}
}Editor is loading...