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