a java application program to multiply two matrices A and B and output the product matrix C. Matrix A must have the same number of columns as matrix B has rows. The maximum matrix sizes are 5

Try this code, pastit as private in your clase or as a static method in other one.

private int[][] matrixMult(int[][] m1, int[][] m2) { if(m1[0].length==m2.length) { int[][] m3 = new int[m1.length][m2[0].length]; for(int i=0;i { for(int j=0;j { for(int k=0;k { m3[i][j] += m1[i][k]*m2[k][j]; } } } return m3; } return null; }

the null happens when the sizes of the matrix are not compatible. n m n m a[2][3]*b[3][1]

as you can see if m in a has the same size of n in b. the calculation can proceed.

n in a and m in b define the new dimensions of the future matrix.

Try this code, pastit as private in your clase or as a static method in other one.

private int[][] matrixMult(int[][] m1, int[][] m2)
{
{
{

{

if(m1[0].length==m2.length)

{

int[][] m3 = new int[m1.length][m2[0].length];

for(int i=0;i

for(int j=0;j

for(int k=0;k

m3[i][j] += m1[i][k]*m2[k][j];

}

}

}

return m3;

}

return null;

}

the null happens when the sizes of the matrix are not compatible.

n m n m

a[2][3]*b[3][1]

as you can see if m in a has the same size of n in b.

the calculation can proceed.

n in a and m in b define the new dimensions of the future matrix.