Skip to content

Commit ad065ed

Browse files
authored
matrix Multiplication in java
1 parent cd3d03b commit ad065ed

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.Scanner;
2+
3+
public class matrix_Multiplication {
4+
public static void main(String args[]) {
5+
6+
int m, n, p, q, sum = 0, c, d, k;
7+
Scanner in = new Scanner(System.in);
8+
9+
System.out.print("Enter Number of Rows and Columns of First Matrix : ");
10+
m = in.nextInt();
11+
n = in.nextInt();
12+
13+
int first[][] = new int[m][n];
14+
15+
System.out.print("Enter First Matrix Elements : ");
16+
17+
for (c = 0; c < m; c++) {
18+
for (d = 0; d < n; d++) {
19+
first[c][d] = in.nextInt();
20+
}
21+
}
22+
23+
System.out.print("Enter Number of Rows and Columns of Second Matrix : ");
24+
p = in.nextInt();
25+
q = in.nextInt();
26+
27+
if (n != p) {
28+
System.out.print("Matrix of the entered order can't be Multiplied..!!");
29+
} else {
30+
int second[][] = new int[p][q];
31+
int multiply[][] = new int[m][q];
32+
33+
System.out.print("Enter Second Matrix Elements :\n");
34+
35+
for (c = 0; c < p; c++) {
36+
for (d = 0; d < q; d++) {
37+
second[c][d] = in.nextInt();
38+
}
39+
}
40+
41+
System.out.print("Multiplying both Matrix...\n");
42+
43+
for (c = 0; c < m; c++) {
44+
for (d = 0; d < q; d++) {
45+
for (k = 0; k < p; k++) {
46+
sum = sum + first[c][k] * second[k][d];
47+
}
48+
49+
multiply[c][d] = sum;
50+
sum = 0;
51+
}
52+
}
53+
54+
System.out.print("Multiplication Successfully performed..!!\n");
55+
System.out.print("Now the Matrix Multiplication Result is :\n");
56+
57+
for (c = 0; c < m; c++) {
58+
for (d = 0; d < q; d++) {
59+
System.out.print(multiply[c][d] + "\t");
60+
}
61+
System.out.print("\n");
62+
}
63+
}
64+
65+
}
66+
}

0 commit comments

Comments
 (0)