-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
80 lines (79 loc) · 1.72 KB
/
Copy pathMatrixMultiplication.java
File metadata and controls
80 lines (79 loc) · 1.72 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
package OOPJava;
import java.util.Scanner;
public class MatrixMultiplication{
public static void main (String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the order m1:");
int m1=sc.nextInt();
System.out.println("Enter the order n1:");
int n1=sc.nextInt();
System.out.println("Enter the order m2:");
int m2=sc.nextInt();
System.out.println("Enter the order n2:");
int n2=sc.nextInt();
if (n1!=m2)
{
System.out.println("Matrix Multiplication is not possible");
}
int A[][]=new int [m1][n1];
int B[][]=new int [m2][n2];
int C[][]=new int [m1][n2];
System.out.println("Read matrix A");
for(int i=0;i<m1;i++)
{
for (int j=0;j<n1;j++)
{
System.out.println("A["+i+"]["+j+"]=");
A[i][j]=sc.nextInt();
}
}
System.out.println("Read matrix B");
for (int i=0;i<m2;i++)
{
for (int j=0;j<n2;j++)
{
System.out.println("B["+i+"]["+j+"]=");
B[i][j]=sc.nextInt();
}
}
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]+=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();
}
}
}