-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
56 lines (49 loc) · 1.97 KB
/
Matrix.java
File metadata and controls
56 lines (49 loc) · 1.97 KB
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
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix:");
int m = sc.nextInt();
int n = sc.nextInt();
int[][] firstMatrix = new int[m][n];
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
firstMatrix[i][j] = sc.nextInt();
}
}
System.out.println("Enter the number of rows and columns of second matrix:");
int p = sc.nextInt();
int q = sc.nextInt();
int[][] secondMatrix = new int[p][q];
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < p; i++) {
for (int j = 0; j < q; j++) {
secondMatrix[i][j] = sc.nextInt();
}
}
if (n != p) {
System.out.println("Matrices can't be multiplied with each other.");
} else {
int[][] resultMatrix = new int[m][q];
System.out.println("Addition of two matrices:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
resultMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}
System.out.println("Multiplication of two matrices:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < q; j++) {
for (int k = 0; k < n; k++) {
resultMatrix[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();
}
}
}
}