diff --git a/src/JavaTrainingTask/Question9/MatrixMultiplication.java b/src/JavaTrainingTask/Question9/MatrixMultiplication.java new file mode 100644 index 0000000000000000000000000000000000000000..a427185d9aa3fc45c69987ca9769852257230b76 --- /dev/null +++ b/src/JavaTrainingTask/Question9/MatrixMultiplication.java @@ -0,0 +1,51 @@ +package JavaTrainingTask.Question9; +import java.util.Scanner; +public class MatrixMultiplication { + + +MatrixMultiplication() { + System.out.println("object is created"); +} + + public static void inputMatrix(int[][] matrix, int rows, int cols, Scanner input, String matrixName) + { + System.out.println("Enter the elements of the " + matrixName + " matrix:"); + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + matrix[i][j] = input.nextInt(); + } + } + } + + public static void multiplyMatrices(int[][] matrixA, int[][] matrixB, int[][] matrixC, int m, int n, int p) + { + for (int i = 0; i < m; i++) + { + for (int j = 0; j < p; j++) + { + matrixC[i][j] = 0; + for (int k = 0; k < n; k++) + { + matrixC[i][j] += matrixA[i][k] * matrixB[k][j]; + } + } + } + } + + public static void displayMatrix(int[][] matrix, int rows, int cols) + { + System.out.println("The product of the matrices is:"); + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + System.out.print(matrix[i][j] + " "); + } + System.out.println(); + } + } + } + + diff --git a/src/JavaTrainingTask/Question9/main.java b/src/JavaTrainingTask/Question9/main.java new file mode 100644 index 0000000000000000000000000000000000000000..c161305522f4bf60a9cc53384e2e1c09d0632e8b --- /dev/null +++ b/src/JavaTrainingTask/Question9/main.java @@ -0,0 +1,31 @@ +package JavaTrainingTask.Question9; +import java.util.Scanner; +public class main { + public static void main(String[] args) + { + + + MatrixMultiplication obj = new MatrixMultiplication(); + Scanner input = new Scanner(System.in); + + System.out.println("Enter the number of rows of the first matrix:"); + int m = input.nextInt(); + System.out.println("Enter the number of columns of the first matrix (and rows of the second):"); + int n = input.nextInt(); + + System.out.println("Enter the number of columns of the second matrix:"); + int p = input.nextInt(); + + int[][] matrixA = new int[m][n]; + int[][] matrixB = new int[n][p]; + int[][] matrixC = new int[m][p]; + + obj.inputMatrix(matrixA, m, n, input, "first"); + obj.inputMatrix(matrixB, n, p, input, "second"); + + obj.multiplyMatrices(matrixA, matrixB, matrixC, m, n, p); + + obj.displayMatrix(matrixC, m, p); + + } +}