Jul 10, 2007

MULTIPLICATION OF TWO MATRICES IN JAVA:

class matmul1
{
public static void main(String[] args)
{
int p[][],q[][],r[][],s[][],temp=0,i,j,k,temp1=0;

p=new int[3][3];
q=new int[3][3];
r=new int[3][3];
s=new int[3][3];

for (i=0;i<=2;i++) // first matrix input
{
for (j=0;j<=2 ;j++)
{
p[i][j] = i;
}
}

for (i=0;i<=2;i++) // first matrix output
{
for (j=0;j<=2 ;j++)
{
System.out.print(p[i][j]+" ");
}
System.out.println();
}



System.out.println();


for (i=0;i<=2;i++) // second matrix input
{
for (j=0;j<=2 ;j++)
{
q[i][j] = j;
}
}

for (i=0;i<=2;i++) // second matrix output
{
for (j=0;j<=2 ;j++)
{
System.out.print(q[i][j]+" ");
}
System.out.println();
}


System.out.println();


for (k=0;k<=2 ;k++) // multiplication of matrix
{
for (i=0;i<=2;i++)
{
for (j=0;j<=2 ;j++)
{
temp=p[k][j]*q[j][i];
temp1=temp+temp1;
}
System.out.println(temp1);
r[k][i]=temp1;
temp1=0;
}
}


for (i=0;i<=2;i++) // output of multiply.
{
for (j=0;j<=2 ;j++)
{
System.out.print(r[i][j]+" ");
}
System.out.println();
}
}
}

No comments:

Post a Comment

Infolinks In Text Ads

Total Pageviews

Powered by Blogger.

Dont Forget To Follow Us

Blog Archive