Advertisement
d1i2p3a4k5

6.matrix multiplication

Oct 18th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3. class matmul
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         int i,j,k,l,m,n;
  8.         Scanner t = new Scanner (System.in);
  9.         System.out.println("enter row and column of 1st matrix");
  10.         int r1 = t.nextInt();
  11.         int c1 = t.nextInt();
  12.         System.out.println("enter row and column of 2nd matrix");
  13.         int r2 = t.nextInt();
  14.         int c2 = t.nextInt();
  15.         if(c1==r2)
  16.         {
  17.             int a[][] = new int[r1][c1];
  18.             int b[][] = new int[r2][c2];
  19.             System.out.println("enter elements of 1st matrix");
  20.             for(i=0;i<r1;i++)
  21.             {
  22.                 for(j=0;j<c1;j++)
  23.                 {
  24.                     a[i][j] = t.nextInt();
  25.                 }
  26.             }
  27.             System.out.println("enter elements of 2nd matrix");
  28.             for(i=0;i<r2;i++)
  29.             {
  30.                 for(j=0;j<c2;j++)
  31.                 {
  32.                     b[i][j] = t.nextInt();
  33.                 }
  34.             }
  35.             int c[][] = new int[r1][c1];
  36.             for(i=0;i<r1;i++)
  37.             {
  38.                 for(j=0;j<r2;j++)
  39.                 {
  40.                 c[i][j]=0;
  41.                     for(k=0;k<c1;k++)
  42.                     {
  43.                         c[i][j]=c[i][j]+a[i][k]*b[k][j];
  44.                     }
  45.                 }
  46.             }
  47.             System.out.println("multiplication of two matrix is :");
  48.             for(i=0;i<r1;i++)
  49.             {
  50.                 for(j=0;j<c1;j++)
  51.                 {
  52.                     System.out.print(c[i][j]+"  ");
  53.                    
  54.                 }
  55.                 System.out.println();
  56.             }
  57.         }
  58.         else
  59.         System.out.println("multiplication not possible");
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement