# Multiplication of two matrix in java

Java program for Two Matrix Multiplication. Here more solutions.

``````/*
Java program for
Two matrix multiplication
*/
public class Multiply
{
// Display the element of given 2d matrix
public static void printRecord(int[][] matrix)
{
System.out.println("  --------------");
// Assume  N x N Matrix size
int row = matrix.length;
int col = matrix[0].length;
// Iterate the row element
for (int i = 0; i < row; ++i)
{
// Iterate the column element
for (int j = 0; j < col; ++j)
{
// Display element value
System.out.print("  " + matrix[i][j]);
}
System.out.println();
}
System.out.println();
}
public static void multiplication(int[][] a, int[][] b)
{
// Get the size
int row = a.length;
int col = a[0].length;
// This matrix are store the result of multiplication
int[][] result = new int[row][col];
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
// Set the initial value of new matrix element
result[i][j] = 0;
for (int k = 0; k < row; ++k)
{
// Multiply matrix A [i] row and [k] columns to
// the Matrix B [k] columns and [j] rows.
result[i][j] += a[i][k] * b[k][j];
}
}
}
System.out.println("  Matrix A");
// Print element of matrix x
printRecord(a);
System.out.println("  Matrix B");
// Print element of matrix y
printRecord(b);
System.out.println("  Matrix [(A) x (B)]");
// Display resultant matrix
printRecord(result);
}
public static void main(String[] args)
{
// Define matrix A
int[][] a = {
{
1 , 2 , 3
},
{
6 , 1 , 2
},
{
5 , 4 , 3
}
};
// Define matrix B
int[][] b = {
{
3 , 1 , 3
},
{
1 , 1 , 2
},
{
2 , 2 , 3
}
};
multiplication(a, b);
}
}
``````

Output

``````  Matrix A
--------------
1  2  3
6  1  2
5  4  3

Matrix B
--------------
3  1  3
1  1  2
2  2  3

Matrix [(A) x (B)]
--------------
11  9  16
23  11  26
25  15  32
``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.