Matrix Addition and Subtraction
Public class MatrixAddition {
Public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print(“Enter number of rows: “);
int rows = s.nextInt();
System.out.print(“Enter number of columns: “);
int columns = s.nextInt();
int[][] a = new int[rows][columns];
int[][] b = new int[rows][columns];
System.out.println(“Enter the first matrix”);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
a[i][j] = s.nextInt();
}
}
System.out.println(“Enter the second matrix”);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
b[i][j] = s.nextInt();
}
}
int[][] c = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println(“The sum of the two matrices is”);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(c[i][j] + ” “);
}
System.out.println();
}
}
}
Enter number of columns: 3
Enter the first matrix
3 4 7
1 8 4
Enter the second matrix
3 2 1
1 0 4
The sum of the two matrices is
6 6 8
2 8 8
Public class Matrices {
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter number of rows: “);
int rows = scanner.nextInt();
System.out.print(“Enter number of columns: “);
int columns = scanner.nextInt();
System.out.println();
System.out.println(“Enter first matrix”);
int[][] a = readMatrix(rows, columns);
System.out.println();
System.out.println(“Enter second matrix”);
int[][] b = readMatrix(rows, columns);
int[][] sum = add(a, b);
int[][] difference1 = subtract(a, b);
int[][] difference2 = subtract(b, a);
System.out.println();
System.out.println(“A + B =”);
printMatrix(sum);
System.out.println();
System.out.println(“A – B =”);
printMatrix(difference1);
System.out.println();
System.out.println(“B – A =”);
printMatrix(difference2);
}
Public static int[][] readMatrix(int rows, int columns) {
int[][] result = new int[rows][columns];
Scanner s = new Scanner(System.in);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = s.nextInt();
}
}
return result;
}
Public static int[][] add(int[][] a, int[][] b) {
int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
return result;
}
Public static int[][] subtract(int[][] a, int[][] b) {
int rows = a.length;
int columns = a[0].length;
int[][] result = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = a[i][j] – b[i][j];
}
}
return result;
}
Public static void printMatrix(int[][] matrix) {
int rows = matrix.length;
int columns = matrix[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(matrix[i][j] + ” “);
}
System.out.println();
}
}
}
Enter number of columns: 2
Enter first matrix
3 4
7 9
Enter second matrix
1 2
3 4
A + B =
4 6
10 13
A – B =
2 2
4 5
B – A =
-2 -2
-4 -5