-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiplyTwoMatrix.java
More file actions
95 lines (78 loc) · 2.7 KB
/
MultiplyTwoMatrix.java
File metadata and controls
95 lines (78 loc) · 2.7 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
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* PROGRAM : To multiply two matrices using your own method. Print the resultant along with two input matrices.
* FILE : MultiplyTwoMatrix.java
* CREATED BY : Santosh Hembram
* DATE : 12-10-20
*/
import java.util.*;
class MatrixMul {
public void multiplyMatrix(int matrix[][], int matrix2[][]) {
int mulResult[][] = new int[matrix.length][matrix2[0].length];
for (int i=0; i<matrix.length; i++) {
for (int j=0; j<matrix[0].length; j++) {
mulResult[i][j] = 0;
for(int k=0; k<matrix2.length; k++) {
mulResult[i][j] = mulResult[i][j] + matrix[i][k] * matrix2[k][j];
}
}
}
System.out.println("----- Displaying the resultant matrix -------");
for (int i=0; i<matrix.length; i++) {
for (int j=0; j<matrix[0].length; j++) {
System.out.print(mulResult[i][j]+" ");
}
System.out.println();
}
}
}
class MultiplyTwoMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the row size for 1st matrix: ");
int r = sc.nextInt();
System.out.print("Enter the coloumn size for 1st matrix: ");
int c = sc.nextInt();
int mat[][] = new int[r][c];
System.out.println("---------- Enter the elements of the 1st matrix --------------");
for(int i=0; i<r; i++) {
for (int j=0; j<c; j++) {
System.out.print("Enter the elements for row "+i+" coloumn "+j+": ");
mat[i][j] = sc.nextInt();
}
}
System.out.print("Enter the row size for 2nd matrix: ");
int r2 = sc.nextInt();
System.out.print("Enter the coloumn size for 2nd matrix: ");
int c2 = sc.nextInt();
while (r2!=c) {
System.out.println("WARNING!!!");
System.out.println("The row size must be same with the first Matrix column size !!!!!!!!!!!!!!");
System.out.print("Again enter the coloumn size for 2nd matrix: ");
c2 = sc.nextInt();
}
int mat2[][] = new int[r2][c2];
System.out.println("--------- Enter the elements of the 2nd matrix --------------");
for(int k=0; k<r2; k++) {
for (int l=0; l<c2; l++) {
System.out.print("Enter the elements for row "+k+" coloumn "+l+": ");
mat2[k][l] = sc.nextInt();
}
}
System.out.println("------- Displaying the 1st Matrix ----------");
for( int i=0; i<r; i++) {
for ( int j=0; j<c; j++) {
System.out.print(mat[i][j]+" ");
}
System.out.println();
}
System.out.println("------- Displaying the 2nd Matrix ----------");
for( int k=0; k<r2; k++) {
for ( int l=0; l<c2; l++) {
System.out.print(mat2[k][l]+" ");
}
System.out.println();
}
MatrixMul obj = new MatrixMul();
obj.multiplyMatrix(mat,mat2);
}
}