Problem: Write a Java program to multiply two matrices and output the resultant matrix.


Matrix multiplication in C

Two matrices A and B can only be multiplied, if the number of columns in A is equal to the number of rows in B and the order of the resultant matrix would be (rows of A x columns of B).

Knowing all this, we can multiply two matrix in Java as follows:

  1. Input matrix1 and matrix2
  2. Declare empty resultant matrix of order (r1, c2)
  3. Loop from 0 to result.length as i:
    1. Nest another loop from 0 to result[0].length as j:
      1. Nest another loop from 0 to matrix1[0].length as k:
        1. Sum (matrix1[I][K] * matrix2[K][J])
      2. Store the sum into the resultant matrix as result[i][j]=sum
    2. End loop.
  4. End loop.
  5. Output the resultant matrix.

Here is the Java program that implements the above logic to multiply two matrices:


Resultant Matrix
6 11
3 6

Leave a Reply

twenty − 4 =