-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixMul.java
73 lines (59 loc) · 1.63 KB
/
MatrixMul.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
65
66
67
68
69
70
71
72
73
import java.util.*;
public class MatrixMul
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Dimensions of Matrices: ");
int n = sc.nextInt();
int a[][] = new int[n][n];
int b[][] = new int[n][n];
System.out.println();
System.out.println("Matrix 1: ");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print("Enter element: ");
a[i][j] = sc.nextInt();
}
}
System.out.println();
System.out.println("Matrix 2: ");
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
System.out.print("Enter element: ");
b[i][j] = sc.nextInt();
}
}
System.out.println();
MatrixMul mul = new MatrixMul();
mul.multiply(a,b);
sc.close();
}
public void multiply(int a[][],int b[][])
{
int c[][] = new int[a.length][a.length];
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a.length; j++)
{
for(int k = 0; k < a.length; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
System.out.println("Resultant Matrix: ");
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j < a.length; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
}