JAVA Program to Perform Multiplication of Matrix

JAVA Program to Perform Multiplication of Matrix:


 class Twodimult
 {

  public static void main(String args[])
  {
       int [][] A = {{1,2,3},{4,5,6},{7,8,9}};
       int [][] B = {{1,2,3},{4,5,6},{7,8,9}};
       int C[][] = new int[3][3] ;

      int i,j,k;
      System.out.println("Matrix A is \n");
      for( i=0;i<3;i++)
      {
      for( j=0;j<3;j++)
      {
      System.out.print(" "+A[i][j]);
      }
      System.out.println();
      }


      System.out.println("\nMatrix B is \n");
      for( i=0;i<3;i++)
      {
       for( j=0;j<3;j++)
       {
       System.out.print(" "+B[i][j]);
       }
       System.out.println();
      }

      // Multipication

          for( i=0;i<3;i++)
          {
           for( j=0;j<3;j++)
           {
            for( k=0;k<3;k++)
           {
            C[i][j] = C[i][j]+(A[i][k])*(B[k][j]);
           }
           }
          }

        System.out.println("\nMultiplication is \n");
          for( i=0;i<3;i++)
          {
          for( j=0;j<3;j++)
          {
           System.out.print(" "+C[i][j]);
         //  System.out.format(" %5d", C[i][j]);
          }
           System.out.println();
          }
   }
  }

OUTPUT:

Matrix A is

 1 2 3
 4 5 6
 7 8 9

Matrix B is

 1 2 3
 4 5 6
 7 8 9

Multiplication is

 30  36  42
 66  81  96
 102 126 150

Leave a Reply

Your email address will not be published. Required fields are marked *

four × four =