import java.util.Scanner;

public class MatrixOperations {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println(“Enter the number of rows and columns of the first matrix:”);
int rows1 = scanner.nextInt();
int cols1 = scanner.nextInt();
int[][] matrix1 = new int[rows1][cols1];

System.out.println(“Enter the elements of the first matrix:”);
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols1; j++) {
matrix1[i][j] = scanner.nextInt();
}
}

System.out.println(“Enter the number of rows and columns of the second matrix:”);
int rows2 = scanner.nextInt();
int cols2 = scanner.nextInt();
int[][] matrix2 = new int[rows2][cols2];

System.out.println(“Enter the elements of the second matrix:”);
for (int i = 0; i < rows2; i++) {
for (int j = 0; j < cols2; j++) {
matrix2[i][j] = scanner.nextInt();
}
}

System.out.println(“Choose an operation: 1. Add 2. Subtract 3. Multiply”);
int choice = scanner.nextInt();

try {
switch (choice) {
case 1:
int[][] additionResult = addMatrices(matrix1, matrix2);
System.out.println(“Result of Addition:”);
printMatrix(additionResult);
break;
case 2:
int[][] subtractionResult = subtractMatrices(matrix1, matrix2);
System.out.println(“Result of Subtraction:”);
printMatrix(subtractionResult);
break;
case 3:
int[][] multiplicationResult = multiplyMatrices(matrix1, matrix2);
System.out.println(“Result of Multiplication:”);
printMatrix(multiplicationResult);
break;
default:
System.out.println(“Invalid choice.”);
}
} catch (IllegalArgumentException e) {
System.out.println(“Error: ” + e.getMessage());
}
}

public static int[][] addMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;

if (rows1 != rows2 || cols1 != cols2) {
throw new IllegalArgumentException(“Matrices must have the same dimensions for addition.”);
}

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 int[][] subtractMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;

if (rows1 != rows2 || cols1 != cols2) {
throw new IllegalArgumentException(“Matrices must have the same dimensions for subtraction.”);
}

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 int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int rows2 = matrix2.length;
int cols2 = matrix2[0].length;

if (cols1 != rows2) {
throw new IllegalArgumentException(“Number of columns of the first matrix must equal the number of rows of the second matrix for multiplication.”);
}

int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

return result;
}

public static void printMatrix(int[][] matrix) {
for (int[] row: matrix) {
for (int element: row) {
System.out.print(element + ” “);
}
System.out.println();
}
}
}


Leave a Reply

Your email address will not be published. Required fields are marked *