# Matrix Multiplication In Java

Sharing is healthy !!

Remember matrix multiplication we used to do in school…(maybe 5th or 6th grade). Well, now we are going to demonstrate matrix multiplication in java.

Basically to perform a matrix multiplication column of the first matrix should be equal to row of the second matrix. Let’s get into code implementation,

#### example: Matrix Multiplication In Java

Also Read – Char Array to String Java Program

```import java.util.Scanner;

public class Matrix_Multiplication_In_Java {

public static void main(String[] args) {

int x;

Scanner sc = new Scanner(System.in);
System.out.println("Please enter base of squared matrix: ");
x = sc.nextInt();

int l[][] = new int[x][x];
int m[][] = new int[x][x];
int n[][] = new int[x][x];

System.out.println("Please enter elements of first matrix(rowwise)");
for(int a = 0;a < x;a++)
{
for(int b = 0;b < x;b++)
{
l[a][b] = sc.nextInt();
}
}

System.out.println("Please enter elements of second matrix(rowwise)");
for(int a = 0;a < x;a++)
{
for(int b = 0;b < x;b++)
{
m[a][b] = sc.nextInt();
}
}

System.out.println("Matrix Multiplication in Java...");
for(int a = 0;a < x;a++)
{
for(int b = 0;b < x;b++)
{
for(int c = 0;c < x;c++)
{
n[a][b] = n[a][b] + l[a][c] * m[c][b];
}
}
}

System.out.println("Matrix Multiplication:");
for(int a = 0;a < x;a++)
{
for(int b = 0;b < x;b++)
{
System.out.println(n[a][b] + " ");
}
System.out.println();
}
sc.close();
}
}```

Output:

Please enter base of squared matrix: 3
Please enter elements of first matrix(rowwise)
8 2 9
2 8 1
2 8 7
Please enter elements of second matrix(rowwise)
2 5 7
6 7 2
3 5 7
Matrix Multiplication in Java…
Matrix Multiplication:
55 99 123
55 71 37
73 101 79

Sharing is healthy !!