public class MatrixSubtraction {
public static void main(String[] args) {
int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int[][] resultMatrix = subtractMatrices(matrix1, matrix2);
System.out.println("Matrix 1:");
printMatrix(matrix1);
System.out.println("Matrix 2:");
printMatrix(matrix2);
System.out.println("Result Matrix:");
printMatrix(resultMatrix);
}
public static int[][] subtractMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int[][] result = new int[rows1][cols1];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
return result;
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}