Sum of two matrix

mail@pastecode.io avatar
unknown
java
a year ago
958 B
0
Indexable
Never
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();
    }
}