Matrix Multiplication in Java



class MatrixMultiplication
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}},b[][]={{1,2,1},{2,4,6},{7,2,5}}, i, j, k;
int c[][]=new int[3][3];

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\t");
for(j=0;j<3;j++)
{
System.out.print(b[i][j]+"\t");
}
System.out.print("\t");
for(j=0;j<3;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.println();
}
}
}


Output:





No comments

Post your comments

Powered by Blogger.