forked from I-RoshanKumar/Beginner_Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixMultiplication.java
75 lines (61 loc) · 1.55 KB
/
MatrixMultiplication.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
74
75
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
int m, n, p, q;
//array declaration
int [][] X = new int [10][10];
int [][] Y = new int [10][10];
Scanner sc = new Scanner (System.in);
System.out.print("Enter Dimensions(m*n) of matrix X: ");
//input from user
m = sc.nextInt();
n = sc.nextInt();
System.out.print("Enter Dimensions(p*q) of matrix Y: ");
//input from user
p = sc.nextInt();
q = sc.nextInt();
if(n==p)
{
System.out.print("Enter "+ m*n + " integers of Matrix X: ");
for(int i=0 ; i<m ; i++) {
for(int j=0 ; j<n ; j++) {
X[i][j] = sc.nextInt();
}
}
System.out.print("Enter "+ p*q + " integers of Matrix Y: ");
for(int i=0 ; i<p ; i++) {
for(int j=0 ; j<q ; j++) {
Y[i][j] = sc.nextInt();
}
}
System.out.println("\nMatrix X = ");
for(int i=0; i<m;i++) {
for(int j=0; j<n;j++) {
System.out.print(X[i][j]+" ");
}
System.out.println();
}
System.out.println("\nMatrix Y = ");
for(int i=0; i<p;i++) {
for(int j=0; j<q;j++) {
System.out.print(Y[i][j]+" ");
}
System.out.println();
}
int [][] C = new int[m][q];
System.out.println("\nProduct Matrix = ");
for(int i=0; i<m; i++) {
for(int j=0; j<q; j++) {
for(int k=0; k<p;k++) {
C[i][j] += X[i][k]*Y[k][j];
}
System.out.print(C[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("\nMatrix Multiplication not possible...");
sc.close();
}
}