Multiplication of two matrix in java

In our last post we have learnt about the addition of two matrices.Today we shall learn about the multiplication of two matrices.Multiplication of two matrices is little complicated as compared to the addition of the two matrices.One of the basic condition for the multiplication of two matrices is number of columns of the first matrix equal to the number of rows of second matrix.This is the main condition for the multiplication of two matrices.
1. Input:We enter the number of rows,columns of two matrices.Later we enter the elements of the two matrices.
2. Explanation:while multiplying two matrices,we need to multiply the rows,columns.To get the element of the first row,first column element of the resultant matrix……we need to multiply the corresponding first row elements of first matrix with first column elements of second matrix and then add them.In the same way if we need to get the nth row,ith column…..then we need to multiply the corresponding nth row elements of the first matrix with ith column of the second matrix and then add them.
download (1)
From the above picture we can explain that we got the first row,first column element as 1*5+7*2=19….In the same way first row,second column element as 1*6+2*8=22.In the same way we can get the value of the element we need.
3.Output:We need to display the resultant matrix of multiplication.
From the above explanation we shall write the code for multiplication.

import java.util.*;
class multiplication
{
 int i,j,sum=0;
 void multi()
   {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows of first matrix:");
      int m=in.nextInt();
      System.out.println("Enter the number of columns of first matrix:");
      int n=in.nextInt();
      int[][] mat1 = new int[m+1][n+1];  
      for(i=1;i < m+1;i++)
       {
          for(j=1;j < n+1;j++)
           {
             System.out.println("Enter the element of "+ i +" row "+ j +" column:");
             mat1[i][j] = in.nextInt();
            }// end of j loop
         }//  end of i loop
      System.out.println("Enter the number of rows of second matrix:");
      int p=in.nextInt();
      System.out.println("Enter the number of columns of second matrix:");
      int q=in.nextInt();
      int[][] mat2 = new int[p+1][q+1];      
      for(i=1;i < p+1;i++)
       {
         for(j=1;j < q+1;j++)
           {
             System.out.println("Enter the element of "+ i +" row "+ j +" column:");
             mat2[i][j] = in.nextInt();
            }// end of j loop
         }// end of i loop
     if(n==q)//comparing coulmns with rows
      {
       int[][] resltmat = new int[m+1][q+1];
       for ( i = 1 ; i < m+1 ; i++ )
        {
         for ( j = 1 ; j < n+1 ; j++ )
          {  
            for (int k = 0 ; k < p+1 ; k++)
               sum = sum + mat1[i][k]*mat2[k][j];
            resltmat[i][j] = sum;
            sum = 0;
          } // end of the loop
        } // end of the loop
       System.out.println("Resultant matrix is:\n");
       for(i=1;i < m+1;i++)
        {
           for(j=1;j< q+1;j++)
            {
              System.out.print(resltmat[i][j] + "  ");
             } // end of j loop
          System.out.println("\n");
        } // end of i loop
     }
    else
         System.out.println("Matrix Multiplication can't be performed because columns not equal to rows");
   }//end of function
}// end of class
class matmul
{
   public static void main(String arg[])
     {
       Scanner in = new Scanner(System.in);
       multiplication ob = new multiplication();
       ob.multi();
     }
} 

Output:
matmul1

matmul2

You can download the source code:download

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in Java and tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

1 Response to Multiplication of two matrix in java

  1. Sangita says:

    Well written post with good indentation and comments. Check line number 8,10,17,21,23,30 and 58. These statements are not completely visible. Please take care of this.
    Read this :A short article on how to learn java programming
    Java programming tutorial

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s