Site icon WebArtDeveloper

How to Create a Program to Perform Matrix Multiplication in Core Java

In this tutorial how to Perform Matrix Multiplication in Core Java  is shown.

Code:

MatrixMulti.java

class Matrix
{
public static void main(String args[])
{
int m=3,n=3,x=3,y=3;
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,2,3},{4,5,6},{7,8,9}};
int c[][]=new int[m][y];

if(m!=y)
System.out.println(“Multiplication not possible because Row of 1st matrix an Col of 2nd matrix must be same”);
else
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<m;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
/* c[i][j]= a[i][j]+b[i][j];
c[i][j]= a[i][j]-b[i][j];
c[i][j]= a[i][j]/b[i][j]; */
}
}

System.out.println(“Matrix-A: “);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(a[i][j]+” “);
}
System.out.println();
}

System.out.println(“Matrix-B: “);
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(b[i][j]+” “);
}
System.out.println();
}

System.out.println(“Matrix Multiplication: “);
for(int i=0;i<m;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(c[i][j]+” “);
}
System.out.println();
}
}
}

}

https://youtu.be/IFh7tWr22YI

Exit mobile version