Given two matrices A and B of any size, the task to multiply them in Java.
Examples:
Input: A[][] = {{1, 2},
{3, 4}}
B[][] = {{1, 1},
{1, 1}}
Output: {{3, 3},
{7, 7}}
Input: A[][] = {{2, 4},
{3, 4}}
B[][] = {{1, 2},
{1, 3}}
Output: {{6, 16},
{7, 18}}
Approach:
- Take the two matrices to be multiplied
- Check if the two matrices are compatible to be multiplied
- Create a new Matrix to store the product of the two matrices
- Traverse each element of the two matrices and multiply them. Store this product in the new matrix at the corresponding index.
- Print the final product matrix
Below is the implementation of the above approach:
// Java program to multiply two square matrices. import java.io.*; class GFG { // Function to print Matrix static void printMatrix(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(); } } // Function to multiply // two matrices A[][] and B[][] static void multiplyMatrix( 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:"); printMatrix(A, row1, col1); System.out.println("\nMatrix B:"); printMatrix(B, row2, col2); // Check if multiplication is Possible if (row2 != col1) { System.out.println( "\nMultiplication Not Possible"); return; } // Matrix to store the result // The product matrix will // be of size row1 x col2 int C[][] = new int[row1][col2]; // Multiply the two marices 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]; } } // Print the result System.out.println("\nResultant Matrix:"); printMatrix(C, row1, col2); } // Driver code public static void main(String[] args) { int row1 = 4, col1 = 3, row2 = 3, col2 = 4; int A[][] = { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 } }; int B[][] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 } }; multiplyMatrix(row1, col1, A, row2, col2, B); } } |
chevron_right
filter_none
Output:
Matrix A: 1 1 1 2 2 2 3 3 3 4 4 4 Matrix B: 1 1 1 1 2 2 2 2 3 3 3 3 Resultant Matrix: 6 6 6 6 12 12 12 12 18 18 18 18 24 24 24 24
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to multiply two matrices
- Python program to multiply two matrices
- Program to concatenate two given Matrices of same size
- Java Program to Add two Matrices
- Queries on number of Binary sub-matrices of Given size
- Java Program for Maximum size square sub-matrix with all 1s
- Program for subtraction of matrices
- Program for addition of two matrices
- Python program to add two Matrices
- Program to check if two given matrices are identical
- C Program to Multiply two Floating Point Numbers
- Python List Equality | Program to check if two given matrices are identical
- Program to multiply two Matrix by taking data from user
- BigDecimal multiply() Method in Java
- BigInteger multiply() Method in Java with Examples
- C/C++ program to find the size of int, float, double and char
- Program that receives a number and prints it out in large size
- Set size() method in Java with Example
- PriorityQueue size() Method in Java
- Size of file on the Internet using Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

