-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixmutiplication.java
More file actions
51 lines (51 loc) · 1.95 KB
/
matrixmutiplication.java
File metadata and controls
51 lines (51 loc) · 1.95 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
import java.util.Scanner;
public class matrixmutiplication {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int row1,row2,col1,col2;
System.out.println("Enter the number of rows of matrix1");
row1=sc.nextInt();
System.out.println("Enter the number of colunm of matrix1");
col1=sc.nextInt();
System.out.println("Enter the number of rows of matrix2");
row2=sc.nextInt();
System.out.println("Enter the number of column of matrix2");
col2=sc.nextInt();
int[][] mat1=new int[row1][col1];
int[][] mat2=new int[row2][col2];
int[][] mat3=new int[row1][col2];
if (col1!=row2) {
System.out.println("Matrix Mutiplication Not Possible");
}else{
System.out.println("Matrix1");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
System.out.println("Enter the value at position["+i+"]["+j+"]");
mat1[i][j]=sc.nextInt();
}
}
System.out.println("Matrix2");
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
System.out.println("Enter the value at position["+i+"]["+j+"]");
mat2[i][j]=sc.nextInt();
}
}
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
for (int j2 = 0; j2 < col1; j2++) {
mat3[i][j]+=mat1[i][j2]*mat2[j2][j];
}
}
}
System.out.println("Matrix Mutiplication result");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
System.out.print(mat3[i][j]+"\t");
}
System.out.println();
}
}
sc.close();
}
}