-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMatrixMul.java
More file actions
80 lines (79 loc) · 2.03 KB
/
MatrixMul.java
File metadata and controls
80 lines (79 loc) · 2.03 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
80
//Java Program to find MAtrix Multiplication
import java.util.Scanner;
class MatrixMul
{
public static void main(String args[])
{
int m1,n1,m2,n2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of the matrix1:");
m1=sc.nextInt();
n1=sc.nextInt();
System.out.println("Enter the Size of the matrix2:");
m2=sc.nextInt();
n2=sc.nextInt();
int A[][]=new int[m1][n1];
int B[][]=new int[m2][n2];
int C[][]=new int[m1][n2];
if(n1!=m2)
System.out.println("Matrix multiplication not possible");
else
{
System.out.println("Enter the value of Matrix1");
for(int i=0;i<m1;i++)
{
for(int j=0;j<n1;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("Enter the value of Matrix2");
for(int i=0;i<m2;i++)
{
for(int j=0;j<n2;j++)
{
B[i][j]=sc.nextInt();
}
}
System.out.println("Enter the value of Resultant Matrix");
for(int i=0;i<m1;i++)
{
for(int j=0;j<n2;j++)
{
C[i][j]=0;
for(int k=0;k<n1;k++)
{
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}
}
System.out.println("MATRIX A");
for(int i=0;i<m1;i++)
{
for(int j=0;j<n1;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("MATRIX B");
for(int i=0;i<m2;i++)
{
for(int j=0;j<n2;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
System.out.println("MATRIX C");
for(int i=0;i<m1;i++)
{
for(int j=0;j<n2;j++)
{
System.out.print(C[i][j]+"\t");
}
System.out.println();
}
}
}
}