My code is super easy and works for any order of matrix
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Enter No. of rows in matrix 1 : ");
int arows = sc.nextInt();
System.out.println(" Enter No. of columns in matrix 1 : ");
int acols = sc.nextInt();
System.out.println(" Enter No. of rows in matrix 2 : ");
int brows = sc.nextInt();
System.out.println(" Enter No. of columns in matrix 2 : ");
int bcols = sc.nextInt();
if (acols == brows) {
System.out.println(" Enter elements of matrix 1 ");
int a[][] = new int[arows][acols];
int b[][] = new int[brows][bcols];
for (int i = 0; i < arows; i++) {
for (int j = 0; j < acols; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println(" Enter elements of matrix 2 ");
for (int i = 0; i < brows; i++) {
for (int j = 0; j < bcols; j++) {
b[i][j] = sc.nextInt();
}
}
System.out.println(" The Multiplied matrix is : ");
int sum = 0;
int c[][] = new int[arows][bcols];
for (int i = 0; i < arows; i++) {
for (int j = 0; j < bcols; j++) {
for (int k = 0; k < brows; k++) {
sum = sum + a[i][k] * b[k][j];
c[i][j] = sum;
}
System.out.print(c[i][j] + " ");
sum = 0;
}
System.out.println();
}
} else {
System.out.println("Order of matrix in invalid");
}
}