//Java program to Perform Matrix
Multiplication
import java.util.*;
class multi
{
Scanner s=new Scanner(System.in);
int a[][],b[][],r1,c1,r2,c2,i,j,k;
public void get()
{
System.out.println(" MATRIX MULTIPLICATION ");
System.out.println(" ~~~~~~ ~~~~~~~~~~~~~~ ");
System.out.println("Enter the Number of Rows for first matrix : ");
r1=s.nextInt();
System.out.println("Enter the Number of Column for first matrix :
");
c1=s.nextInt();
System.out.println("Enter the Number of Rows for Second matrix : ");
r2=s.nextInt();
System.out.println("Enter the Number of Column for Second matrix :
");
c2=s.nextInt();
}
if(c1!=r2)
{
System.out.println("Matrix multiplication is NOT POSSIBLE");
return;
}
else
{
a=new int [r1][c1];
b=new int [r2][c2];
a=getdata(r1,c1);
b=getdata(r2,c2);
c=matrixmul(a,b,r1,c1,r2,c2);
display(a,r1,c1);
display(b,r2,c2);
display(c,c1,r2);
}
int[][]getdata(int row,int col)
{
int a1[][];
a1=new int [row][col];
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{
a[i][j]=s.nextInt();
}
return a1;
}
void display(int a2[][],int row,int col)
{
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
System.out.println(a2[i][j]+" ");
}
int[][]matrixmul(int a3[][],int a4[][],int
x1,int y1,int x2,int y2)
{
int res[][];
res=new int [y1][x2];
for(i=0; i<x2; i++)
{
for(j=0; j<y1; j++)
{
res[i][j]=0;
for(k=0;k<y2;k++)
res[i][j]+=a3[i][k]*a4[k][j];
}
}
return res;
}
}
class mat
{
public static void main(String args[])
{
matrixmul m=new matrixmul();
m.get();
m.display();
}
}
No comments:
Post a Comment