# Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are multiplied by the column element.

To perform multiplication in matrices, the column number of the first matrix is equal to the row number of the second matrix.

Example

1. A [] [] = {{2,3}, {8,6}}
B [] [] = {{6,0}, {0,3}}
C [] [] = {{12,21}, {48,18}}

Explanation

In the above example, the order of both matrices is the same i.e., 2 X 2 and the column number is equal to the row number in the second matrix.

Example

1. A [] [] = {{2,3}, {8,6}}
B [] [] = {{6,0}, {0,3}, {5,7}}

Explanation

In the above example, the number of columns in the first matrix is not equal to the number of rows in the second matrix.

Approach for matrix multiplication

• Take two matrices
• Check whether the matrices can be multiplied or not.
• Construct a new matrix of order (row size of first column X column size of the second matrix)
• Traverse through each element of the first matrix row-wise and multiply with the column element of second matrix and store the sum value in new matrix constructed.
• Print the final matrix constructed.

Program for multiplication of two matrices using static method

MatrixMultiplication.java

``````import java.io.*;
import java.util.*;
class  MatrixMultiplication {
// Function to print Matrix
static void matrixPrint(int M[][],int rowSize,int colSize)
{
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
System.out.print(M[i][j] + " ");
System.out.println();
}
}
// Multiplication of matrices A[][] and B[][]
static void multiply(int row1, int col1, int A[][],int row2, int col2, int B[][])
{
int i, j, k;
// Print the matrices A and B
System.out.println("\nMatrix A:");
matrixPrint(A, row1, col1);
System.out.println("\nMatrix B:");
matrixPrint(B, row2, col2);
// Check whether the multiplication is possible or not
if (row2 != col1) {
System.out.println("\nMultiplication of given matrices is not Possible");
return;
}
// Construct a new matrix with size of row1 x col2
int C[][] = new int[row1][col2];
// Multiply the two matrices
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Printing the result
System.out.println("\nResultant Matrix:");
matrixPrint(C, row1, col2);
}
// Main Function
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int row1=4;
int  col1 = 3, row2 = 3, col2 = 4;
int A[][] = { { 3, 2, 2 },{ 8, 4,6 },{ 5, 4,9 },{ 2, 5,7} };
int B[][] = { { 3, 5, 2, 1 },{ 1, 3,9, 8 },{ 3, 5, 6,8 } };
multiply(row1, col1, A,row2, col2, B);
}
}``````

Output ### Program for multiplication of two matrices using dynamic method

MatrixMultiply.java

``````import java.io.*;
import java.util.*;
class  Main{
// Function to print Matrix
static void matrixPrint(int M[][],
int rowSize,
int colSize)
{
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
System.out.print(M[i][j] + " ");
System.out.println();
}
}
// Multiplication of matrices A[][] and B[][]
static void multiply(
int row1, int col1, int A[][],
int row2, int col2, int B[][])
{
int i, j, k;
// Print the matrices A and B
System.out.println("\nMatrix A:");
matrixPrint(A, row1, col1);
System.out.println("\nMatrix B:");
matrixPrint(B, row2, col2);

// Check whether the multiplication is possible or not
if (row2 != col1) {
System.out.println("\nMultiplication of given matrices is not Possible");
return;
}

// Construct a new matrix with size of row1 x col2
int C[][] = new int[row1][col2];

// Multiply the two matrices
for (i = 0; i < row1; i++) {
for (j = 0; j < col2; j++) {
for (k = 0; k < row2; k++)
C[i][j] += A[i][k] * B[k][j];
}
}
// Printing the result
System.out.println("\nResultant Matrix:");
matrixPrint(C, row1, col2);
}

// Main Function
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
int row1 = sc.nextInt();
int  col1 = sc.nextInt();
int row2 = sc.nextInt();
int col2 = sc.nextInt();

int A[][] = new int[row1][col1];
int B[][] = new int[row2][col2];
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
A[i][j]=sc.nextInt();
}
}
for(int i=0;i<row1;i++)
{
for(int j=0;j<col1;j++)
{
B[i][j]=sc.nextInt();
}
}
multiply(row1, col1, A,
row2, col2, B);
}
}
``````

Output: