-
Notifications
You must be signed in to change notification settings - Fork 361
/
Copy pathMatirixMultiplication
60 lines (48 loc) · 1.2 KB
/
MatirixMultiplication
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
54
55
56
57
58
59
60
import java.util.Scanner;
import java.lang.*;
public class MatrixMultiplication
{
public static void main(String args[])
{
int r1, r2, c1, c2;
Scanner input = new Scanner(System.in);
System.out.println("Enter matrix 1 order: ");
r1 = input.nextInt();
c1 = input.nextInt();
System.out.println("Enter matrix 2 order: ");
r2 = input.nextInt();
c2 = input.nextInt();
if(r1 != c2){
System.out.println("Matrix Order doesn't match, exiting..");
System.exit(1);
}
int mat1[][] = new int[r1][c1];
int mat2[][] = new int[r2][c2];
int out[][] = new int[r1][c2];
System.out.println("Enter matrix 1 elements: ");
for(int i=0; i<r1; i++) {
for(int j=0; j<c1; j++) {
mat1[i][j] = input.nextInt();
}
}
System.out.println("Enter matrix 1 elements: ");
for(int i=0; i<r2; i++) {
for(int j=0; j<c2; j++) {
mat2[i][j] = input.nextInt();
}
}
System.out.println();
System.out.println("Final matrix: ");
for(int i=0;i<r1;i++){
for(int j=0;j<c2;j++){
out[i][j]=0;
for(int k=0;k<r2;k++)
{
out[i][j]+=mat1[i][k]*mat2[k][j];
}
System.out.print(out[i][j]+" ");
}
System.out.println();
}
}
}