-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.java
54 lines (51 loc) · 1.75 KB
/
matrix.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
import java.util.Scanner;
public class matrix {
public static void main(String args[]) {
int i, j, k, rowF, rowS, colF, colS;
int first[][] = new int[10][10];
int second[][] = new int[10][10];
int product[][] = new int[10][10];
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Rows and Cols of First Matrix");
rowF = scanner.nextInt();
colF = scanner.nextInt();
System.out.println("Enter Elements of First Matrix");
for (i = 0; i < rowF; i++) {
for (j = 0; j < colF; j++) {
first[i][j] = scanner.nextInt();
}
}
System.out.println("Enter Rows and Cols of Second Matrix");
rowS = scanner.nextInt();
colS = scanner.nextInt();
System.out.println("Enter Elements of Second Matrix");
for (i = 0; i < rowS; i++) {
for (j = 0; j < colS; j++) {
second[i][j] = scanner.nextInt();
}
}
for (i = 0; i < rowF; i++) {
for (j = 0; j < colF; j++) {
for (k = 0; k < colS; k++) {
product[i][j] += first[i][k] * second[k][j];
}
}}
System.out.println("Product Matrix");
for (i = 0; i < rowF; i++)
{
for (j = 0; j < colS; j++)
{
System.out.print(product[i][j]);
System.out.print(" ");
}
System.out.print("\n");
}
System.out.println("transpose Matrix");
for (i = 0; i < rowF; i++) {
for (j = 0; j < colS; j++) {
System.out.print(product[j][i] + " ");
}
System.out.print("\n");
}
}
}