-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix
52 lines (51 loc) · 1.81 KB
/
Matrix
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
package com.company;
public class Matrix {
public static void main(String[] args) {
int sum = 0;
int a[][] = {{1, 3, 4}, {2, 4, 3}, {3, 4, 5}};
int b[][] = {{1, 3, 4}, {2, 4, 3}, {1, 2, 4}};
System.out.println("The first matrix is : ");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("The second matrix is : ");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int matrix[][] = new int[3][3];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The addition of two matrices is : ");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
for (int k = 0; k < 3; k++) {
sum = sum + (a[i][k] * b[k][j]);
}
matrix[i][j] = sum;
sum = 0;
}
}
System.out.println("The multiplication of two matrices is : ");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}