-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matpdt.java
64 lines (47 loc) · 1.58 KB
/
Matpdt.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.*;
class Matpdt {
public static void main(String[] args) {
int i,j,k,m,n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows in mat1: ");
m = sc.nextInt();
System.out.println("Enter the number of columns in mat1: ");
n = sc.nextInt();
System.out.println("Enter the number of rows in mat2: ");
n = sc.nextInt();
System.out.println("Enter the number of columns in mat2: ");
m = sc.nextInt();
int[][] mat1 = new int[m][n];
int[][] mat2 = new int[n][m];
int[][] pdt = new int[m][m];
System.out.println("Enter the elements of mat1: \n");
for(i=0; i<m ; i++){
for(j=0; j<n ; j++){
System.out.println(" ");
mat1[i][j] = sc.nextInt();
}
}
System.out.println("Enter the elements of mat2: \n");
for(i=0; i<n ; i++){
for(j=0; j<m ; j++){
System.out.println(" ");
mat2[i][j] = sc.nextInt();
}
}
for(i=0; i<m; i++){
for(j=0; j<m; j++){
pdt[i][j]=0;
for(k=0; k<n; k++){
pdt[i][j]+= mat1[i][k]*mat2[k][j];
}
}
}
System.out.println("The product of the two matrices: ");
for(i=0; i<m ; i++){
for(j=0; j<m ; j++){
System.out.print(pdt[i][j] + " ");
}
System.out.println();
}
}
}