-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmultiplication.java
More file actions
53 lines (44 loc) · 1.67 KB
/
matrixmultiplication.java
File metadata and controls
53 lines (44 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package daaa;
import java.util.Scanner;
public class matrixmultiplication {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of rows of first matrix: ");
int rows1 = scanner.nextInt();
System.out.print("Enter number of columns of first matrix / rows of second matrix: ");
int cols1 = scanner.nextInt();
System.out.print("Enter number of columns of second matrix: ");
int cols2 = scanner.nextInt();
int[][] matrix1 = new int[rows1][cols1];
int[][] matrix2 = new int[cols1][cols2];
int[][] result = new int[rows1][cols2];
System.out.println("Enter elements of 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 elements of second matrix:");
for (int i = 0; i < cols1; i++) {
for (int j = 0; j < cols2; j++) {
matrix2[i][j] = scanner.nextInt();
}
}
// Matrix multiplication
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
System.out.println("Resultant matrix after multiplication:");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
scanner.close();
}
}