public
class
GFG
{
/**
* to find out matrix multiplication
*
* @param matrix1 First matrix
* @param rows1 Number of rows in matrix 1
* @param cols1 Number of columns in matrix 1
* @param matrix2 Second matrix
* @param rows2 Number of rows in matrix 2
* @param cols2 Number of columns in matrix 2
* @return the result matrix (matrix 1 and matrix 2
* multiplication)
*/
public
static
int
[][] matrixMultiplication(
int
[][] matrix1,
int
rows1,
int
cols1,
int
[][] matrix2,
int
rows2,
int
cols2)
throws
Exception
{
if
(cols1 != rows2) {
throw
new
Exception(
"Invalid matrix given."
);
}
int
resultMatrix[][] =
new
int
[rows1][cols2];
for
(
int
i =
0
; i < resultMatrix.length; i++)
{
for
(
int
j =
0
;
j < resultMatrix[i].length;
j++)
{
for
(
int
k =
0
; k < cols1; k++)
{
resultMatrix[i][j]
+= matrix1[i][k] * matrix2[k][j];
}
}
}
return
resultMatrix;
}
public
static
void
main(String[] args)
throws
Exception
{
int
matrix1[][] = { {
2
,
4
}, {
3
,
4
} };
int
matrix2[][] = { {
1
,
2
}, {
1
,
3
} };
int
resultMatrix[][] = matrixMultiplication(
matrix1,
2
,
2
, matrix2,
2
,
2
);
System.out.println(
"Result Matrix is:"
);
for
(
int
i =
0
; i < resultMatrix.length; i++)
{
for
(
int
j =
0
;
j < resultMatrix[i].length;
j++)
{
System.out.print(resultMatrix[i][j] +
" "
);
}
System.out.println();
}
}
}