WRITE A JAVA
PROGRAM to multiply two given matrices.
Program :
import java.util.*; class Test
{ int r1,c1,r2,c2;
Test(int r1,int
c1,int r2,int c2)
{
this.r1=r1;
this.c1=c1;
this.r2=r2;
this.c2=c2;
}
int[ ][ ]
getArray(int r,int c)
{
int arr[][]=new int[r][c];
System.out.println("Enter the elements for "+r+"X"+c+"
Matrix:"); Scanner input=new Scanner(System.in);
for(int
i=0;i<r;i++)
for(int j=0;j<c;j++)
arr[i][j]=input.nextInt();
return arr;
}
int[ ][ ]
findMul(int a[ ][ ],int b[ ][ ])
{
int c[][]=new int[r1][c2];
for (int
i=0;i<r1;i++)
for (int j=0;j<c2;j++)
{
c[i][j]=0;
for (int
k=0;k<r2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
return c;
}
void putArray(int
res[ ][ ])
{
System.out.println ("The resultant
"+r1+"X"+c2+" Matrix is:");
for (int i=0;i<r1;i++)
{
for (int j=0;j<c2;j++)
System.out.print(res[i][j]+" ");
System.out.println();
}
}
} //end of Test class
class MatrixMul {
public static void main(String args[ ])throws IOException
{
Test obj1=new
Test(2,3,3,2);
Test obj2=new Test(2,3,3,2);
int x[ ][ ],y[ ][
],z[ ][ ];
System.out.println("MATRIX-1:");
x=obj1.getArray(2,3); //to get the matrix from user
System.out.println("MATRIX-2:");
y=obj2.getArray(3,2);
z=obj1.findMul(x,y);
obj1.putArray(z);
}
}