forked from SahilMadridista/Matrix_Multiplication_Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMulti.java
More file actions
79 lines (60 loc) · 1.45 KB
/
MatrixMulti.java
File metadata and controls
79 lines (60 loc) · 1.45 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Scanner;
public class MatrixMulti {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q,x,y,z;
System.out.println("Enter the number of rows in first matrix : ");
q=scan.nextInt();
System.out.println("Enter the number of column in first matrix : ");
x=scan.nextInt();
System.out.println("Enter the number of rows in second matrix : ");
y=scan.nextInt();
System.out.println("Enter the number of column in second matrix : ");
z=scan.nextInt();
if(x==y)
{
int[][]a=new int[q][x];
int[][]b=new int[y][z];
int[][]c=new int[q][z];
System.out.println("Enter the first matrix row wise : ");
for(int i=0;i<q;i++)
{
for(int j=0;j<x;j++)
{
a[i][j]=scan.nextInt();
}
}
System.out.println("Enter the second matrix row wise : ");
for(int i=0;i<y;i++)
{
for(int j=0;j<z;j++)
{
b[i][j]=scan.nextInt();
}
}
for(int i=0;i<q;i++)
{
for(int j=0;j<z;j++)
{
for(int k=0;k<x;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("The product matrix is : ");
for(int i=0;i<q;i++)
{
for(int j=0;j<z;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
else
{
System.out.println("Multiplication not possible");
}
}
}