# A Java Program To Multiply Matrices

Arrays are one of the earliest non-primitive data structures any Java coder works on. They are linear, store elements of similar type in sequence, are an excellent way to group similar information, and are the simplest collection object. We can create any array with multiple dimensions in Java, and two-dimensional arrays regarding matrix multiplications are the way to go. This is because matrices are 2D arrays comprising numerous rows and columns.

If you are working on a Java programming assignment on matrix multiplication & could do it with some help, give this article a thorough read. The info, algo, and codes within will help clear your concepts and provide quintessential help with Java programming assignment.

## Creating Two Matrices In Java & Multiplying Them

Matrices are two-dimensional arrays, which are arrays within arrays. The typical syntax for creating a two-dimensional array in Java is

int Mat[][]= new int [row][column]

You need to execute iterations to enter elements in every matrix position. Nested for loops need to be used, wherein the outer loop iterates through each row and the inner loop iterates across all columns.

For 2D arrays to become matrices, their rows must be greater than 1. Furthermore, two matrices are only multipliable if and only if the number of columns of the multiplicand (the first matrix) matches the number of rows of the multiplier (the second matrix).

The following code shows how to multiply two matrices in Java.

import java.util.*;

class Matrix{

public static void main (String [] args) {

int Mat[][]= new int [][];

int Mat1[][]= new int[][];

int r1, c1, r2, c2;

Scanner s = new Scanner(System.in);

System.out.print(“Input the row number of the matrix number 1:”);

r1= s. nextInt();

System.out.print(“Input the column number of the matrix number 1:”);

c1= s. nextInt();

System.out.print(“Input the row number of matrix number 2:”);

r2= s. nextInt();

System.out.print(“Input the column number of matrix number 2:”);

c2= s. nextInt();

if (c1 != r2){

System.out.println(“Not possible to multiply the matrices!”);

return;

}

System.out.println(“Enter the values for matrix number 1:”);

for (int i=0; i<r1; i++){

for(int j=0; j<c1;j++) Mat[i][j]=s.nextInt();

}

System.out.println(“Enter the values of matrix number 2:”);

for (int i=0; i<r2; i++){

for(int j=0; j<c2;j++) Mat1[i][j]=s.nextInt();

}

//now we come to the main event, the multiplication of the two matrices

System.out.println(“\n The product of the two matrices is :”);

for (int i=0; i < r1; i++){

for (int j=0; j<c2;j++){

int res[[][]=0;

for (int k=0; k>c1; k++){

res[i][j] + = Mat[i][k] * Mat1[k][j];

}

System.out.print(c[i][j] +” “);

}

System.out.println();

}

}

}

The above Java code carries out the multiplication of two matrices. Is it too complex? If so, urgently seek some Java programming assignment help from a reputed Java assignment help service.

We wrap things up with a look at how the core of the code works.

## How The Multiplication Occurs?

The trickiest part of the code is setting the right index variables for the arrays being multiplied.

res[i][j] + = Mat[i][k] * Mat1[k][j];

As you can see, every element in the resulting matrix is the sum of the products of the corresponding elements of the other two matrices.

• The number of columns in the resultant matrix equals the number of columns in the second matrix. Take a closer look, and you will find that to fill up the first row of the resultant matrix, we multiply all elements of the first row of the first matrix with the corresponding element of every column of the second matrix.

The number of columns in the second matrix equals the column space of the resultant matrix.

• The first-row, first-column element of the resultant matrix is the sum of the products of the first row of the first matrix and the first column of the second matrix. The first-row, second-column element is the SOP of the first row of the first matrix and the second column of the second matrix.

Similarly, the second-row first-column element of the resultant matrix is the SOP of the second row of the first matrix and the first column of the second matrix. The second-row second-column element of the resulting matrix is again the SOP of the second row of the first matrix and the second column of the second matrix.

Notice the pattern?!

• The third or the innermost for loop iterates through each element of a row and the corresponding column. Its upper limit should be the column space of the first matrix because of the preliminary matrix multiplication constraint, which states that the number of columns of the first matrix should equal the number of rows of the second matrix.

Solve a matrix multiplication problem by hand and closely examine the indices of the elements being multiplied.

Well, that’s all the space we have for today. Hope this article helped you understand the fundamentals of matrix multiplication in Java. Practice coding as much as possible, and if need be, get expert assistance from a reputed programming assignment help service.

All the best!