forked from mattmiller1426/Hacktobber2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_product.java
64 lines (51 loc) · 1.43 KB
/
Matrix_product.java
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
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int r1, c1, r2, c2;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in first matrix: ");
r1 = s.nextInt();
System.out.print("Enter number of columns in first matrix: ");
c1 = s.nextInt();
System.out.print("Enter number of rows in second matrix: ");
r2 = s.nextInt();
System.out.print("Enter number of columns in second matrix: ");
c2 = s.nextInt();
if (c1 != r2)
{
System.out.println("Matrix multiplication is not possible");
return;
}
int a[][] = new int[r1][c1];
int b[][] = new int[r2][c2];
int c[][] = new int[r1][c2];
System.out.println("\nEnter " +(r1*c1)+ " values for matrix A : ");
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
a[i][j] = s.nextInt();
}
System.out.println("\nEnter " +(r2*c2)+ " values for matrix B : ");
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
b[i][j] = s.nextInt();
}
System.out.println("\nMatrix multiplication is : ");
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c2; j++)
{
c[i][j] = 0;
for (int k = 0; k < c1; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
System.out.print(c[i][j] + "\t");
}
System.out.println();
}
}
}