Wednesday, 19 December 2012

Aim: To Write a Java program to multiply two given matrices.


import java.io.*;
class matmul
{
   public static void main(String args[]) throws IOException
     {
int i,j,k,m,n,p,q;
int a[][],b[][],c[][];

InputStreamReader obj=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(obj);
              System.out.println("Give Matrix A Size\n");
m=Integer.parseInt(br.readLine());
n=Integer.parseInt(br.readLine());
a=new int[m][n];
System.out.println("Enter Matrix A : ");
for(i=0;i   {
for(j=0;j              a[i][j]=Integer.parseInt(br.readLine());
   }

System.out.println("Give Matrix  B Size ");
p=Integer.parseInt(br.readLine());
q=Integer.parseInt(br.readLine());
b=new int[p][q];
System.out.println("Enter Matrix B");
for(i=0;i
   {
for(j=0;jb[i][j]=Integer.parseInt(br.readLine());
   }
c=new int[m][n];
if(n==p)
  {
for(i=0;i{    
      for(j=0;j{    
      for(k=0;k         c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}


System.out.println("Matrix Multiplication  ");
for(i=0;i  {
for(j=0;j{
System.out.print(" "+c[i][j]);
}
System.out.println(" ");
}
}

   else
         System.out.println("matrix multiplication is not possible");
}
}

No comments:

Post a Comment