06-04-2011, 01:21 PM
Code:
import java.io.*;
class Mult
{
int c[][]=new int[3][3];
void process(int a[][],int b[][],int m,int n,int p,int q)
{
for(int i=0;i<m;i++)
for(int j=0;j<q;j++)
{
c[i][j]=0;
for(int k=0;k<n;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
System.out.print("Product of two metrices is"+"\n");
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
{
System.out.print(c[i][j]+"\t");
}
System.out.print("\n");
}
}
};
class MatrixMult
{
public static void main(String arg[])throws IOException
{
Mult am=new Mult();
int m,n,p,q,i,j,k;
int a[][]=new int[3][3];
int b[][]=new int[3][3];
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter the order of first matrix");
m=Integer.parseInt(dis.readLine());
n=Integer.parseInt(dis.readLine());
System.out.println("Enter the first matrix");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=Integer.parseInt(dis.readLine());
System.out.println("Enter the order of second matrix");
p=Integer.parseInt(dis.readLine());
q=Integer.parseInt(dis.readLine());
System.out.println("Enter the second matrix");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
b[i][j]=Integer.parseInt(dis.readLine());
am.process(a,b,m,n,p,q);
}
}
OUTPUT
E:\3msc\java\liya>java MatrixMult
Enter the order of first matrix
2
2
Enter the first matrix
2
2
2
2
Enter the order of second matrix
2
2
Enter the second matrix
2
2
2
2
Product of two metrices is
8 8
8 8