-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplication.java
More file actions
82 lines (72 loc) · 2.66 KB
/
Copy pathMultiplication.java
File metadata and controls
82 lines (72 loc) · 2.66 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.*;
public class Multiplication {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows for matrix A: ");
int m1 = sc.nextInt(); // Rows of A
System.out.println("Enter the number of columns for matrix A: ");
int n1 = sc.nextInt(); // Columns of A
System.out.println("Enter the number of rows for matrix B: ");
int m2 = sc.nextInt(); // Rows of B
System.out.println("Enter the number of columns for matrix B: ");
int n2 = sc.nextInt(); // Columns of B
// Check if multiplication is possible
if (n1 != m2) {
System.out.println("Multiplication not possible");
return; // Exit if multiplication is not possible
}
// Initialize matrices
int[][] A = new int[m1][n1];
int[][] B = new int[m2][n2];
int[][] C = new int[m1][n2]; // Result matrix
// Read matrix A
System.out.println("Read matrix A: ");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
System.out.print("A[" + i + "][" + j + "] = ");
A[i][j] = sc.nextInt();
}
}
// Read matrix B
System.out.println("Read matrix B: ");
for (int i = 0; i < m2; i++) {
for (int j = 0; j < n2; j++) {
System.out.print("B[" + i + "][" + j + "] = ");
B[i][j] = sc.nextInt();
}
}
// Matrix multiplication
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
C[i][j] = 0; // Initialize C[i][j]
for (int k = 0; k < n1; k++) {
C[i][j] += A[i][k] * B[k][j]; // Correct indices for multiplication
}
}
}
// Print matrix A
System.out.println("Matrix A is: ");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
// Print matrix B
System.out.println("Matrix B is: ");
for (int i = 0; i < m2; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(B[i][j] + "\t");
}
System.out.println();
}
// Print matrix C (Result)
System.out.println("Matrix C (Result of A * B) is: ");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(C[i][j] + "\t");
}
System.out.println();
}
}
}