Home / JAVA / ISC Class 12 / Program for Matrix Multiplication in JAVA

Program for Matrix Multiplication in JAVA

Matrix Multiplication: Here is given a Java Program for Matrix Multiplication also call matrix product. In other words, Let the two matrix to be multiplied be A and B. Let A is an n × m order matrix and B is an m × p order matrix, their matrix product A*B is an n × p order matrix, in which the m entries across the rows of A are multiplied with the m entries down the columns of B.

Steps for Matrix Multiplication in Java:

Step 1: Make sure that the the number of columns in the 1st matrix equals the number of rows in the 2nd matrix.
Step 2: Multiply the elements of each row of the first matrix by the elements of each column in the second matrix.
Step 3: Add the products.

Matrix Multiplication in Java

 

Example:

matrix multiplication example

Iteration 1

Multiplication of matrix A’s first row with each element of matrix B’s all columns,

1*1+2*5, 1*2+2*6, 1*3+2*7, 1*4+2*8

Iteration 2

Multiplication of matrix A’s second row with each element of matrix B’s all columns,

3*1+4*5, 3*2+4*6, 3*3+4*7, 3*4+4*8

Iteration 3

Multiplication of matrix A’s third row with each element of matrix B’s all columns,

5*1+6*5, 5*2+6*6, 5*3+6*7, 5*4+6*8

Iteration 4

Add all the multiplication and get the matrix with an order 3*4.

Also See:   C program for Kaprekar number

 

  • import java.util.*; 
    class MatrixMultiplication
    {
       public static void main(String args[])
       {
          int a, b, p, q, sum = 0, i, j, k;
     
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter the number of rows and columns of first matrix");
          a = sc.nextInt();
          b = sc.nextInt();
     
          int arr1[][] = new int[a][b];
     
          System.out.println("Enter the elements of first matrix");
     
          for ( i = 0 ; i < a ; i++ )
             for ( j = 0 ; j < b ; j++ )
                arr1[i][j] = sc.nextInt();
     
          System.out.println("Enter the number of rows and columns of second matrix");
          p = sc.nextInt();
          q = sc.nextInt();
     
          if ( b != p )
             System.out.println("Matrices with entered orders can't be multiplied with each other.");
          else
          {
             int arr2[][] = new int[p][q];
             int mult[][] = new int[a][q];
     
             System.out.println("Enter the elements of second matrix");
     
             for ( i = 0 ; i < p ; i++ )
                for ( j = 0 ; j < q ; j++ )
                   arr2[i][j] = sc.nextInt();
     
             for ( i = 0 ; i < a ; i++ )
             {
                for ( j = 0 ; j < q ; j++ )
                {   
                   for ( k = 0 ; k < p ; k++ )
                   {
                      sum = sum + arr1[i][k]*arr2[k][j];
                   }
     
                   mult[i][j] = sum;
                   sum = 0;
                }
             }
     
             System.out.println("Product of entered matrices:-");
     
             for ( i = 0 ; i < a ; i++ )
             {
                for ( j = 0 ; j < q ; j++ )
                   System.out.print(multiply[i][j]+"t");
     
                System.out.print("n");
             }
          }
       }
    }

    Output:

    matrix-multiplication-method 1

  • import java.util.*;
    class MatrixMultiplication
    {
    	public static void main(String args[])
    	{
    		int i, j, k;
    		int arr1[][]=new int[3][3];
    		int arr2[][]=new int[3][3];
    		int mult[][]=new int[3][3];
    		System.out.println("Enter the first matrix:");
    		Scanner sc=new Scanner(System.in);
    		for(i=0;i<3;i++)
    		{
    			for(j=0;j<3;j++)
    			{
    				arr1[i][j]=sc.nextInt();
     			}
    		}
    		System.out.println("Enter the second matrix:");
    		for(i=0;i<3;i++)
    		{
    			for(j=0;j<3;j++)
    			{
    				arr2[i][j]=sc.nextInt();
    			}
    		}
    		System.out.println("Matrix multiplication is as follows:");
    		for(i=0;i<3;i++)
    		{
    			for(j=0;j<3;j++)
    			{
    				mult[i][j]=0;
    				for(k=0;k<3;k++)
    				{
    				  	mult[i][j]+=arr1[i][k]*arr2[k][j];
    	           			}
    			}
    	    	}
    	    	for(i=0;i<3;i++)
    		{
    			for(j=0;j<3;j++)
     			{
         				System.out.print(arr1[i][j]+"t");
        			}
    			System.out.println("n");
            		}
            		System.out.println("n");
            		for(i=0;i<3;i++)
    		{
    			for(j=0;j<3;j++)
    	 		{
    	     			System.out.print(arr2[i][j]+"t");
        			}
        			System.out.println("n");
            		}	
            		System.out.println("n");
    	    	for(i=0;i<3;i++)
    			{
    			for(j=0;j<3;j++)
    	 		{
    	     			System.out.print(mult[i][j]+"t");
        			}
        			System.out.println("n");
            		}		   	
    	}
    }

     Output:

    Enter the first matrix:
    1 2 3 4 5 6 7 8 9

    Enter the second matrix:
    9 8 7 6 5 4 3 2 1

    Matrix multiplication is as follows:
    1 2 3
    4 5 6
    7 8 9

    9 8 7
    6 5 4
    3 2 1

    30 24 18
    84 69 54
    138 114 90

  • import java.util.*;
    class MatrixMultiplication
    {
        void printMatrix(int arr[][], int m, int n) // Funtion for printing an array
        {
            for(int i=0; i<m; i++)
            {
                for(int j=0; j<n; j++)
                {
                    System.out.print(arr[i][j]+"t");
                }
                System.out.println();
            }
        }
         
        public static void main(String args[])throws Exception
        {
            MatrixMultiplication ob = new MatrixMultiplication();
            Scanner sc = new Scanner(System.in);
             
            System.out.print("Enter no. of rows of 1st Matrix : ");
            int m1=sc.nextInt();
            System.out.print("Enter no. of columns of 1st Matrix : ");
            int n1=sc.nextInt();
             
            System.out.print("Enter no. of rows of 2nd Matrix : ");
            int m2=sc.nextInt();
            System.out.print("Enter no. of columns of 2nd Matrix : ");
            int n2=sc.nextInt();
             
            if(n1 != m2) 		// check Condition for Multiplication to be possible
            {
                System.out.println("Matrix Multiplication of the given order is not possible");
            }
            else
            {
                int arr1[][]=new int[m1][n1]; 	// Array to store 1st Matrix
                int arr2[][]=new int[m2][n2]; 	// Array to store 2nd Matrix
                int mult[][]=new int[m1][n2]; 	// Array for store Result of matrix Multiplication
                System.out.println("Inputting the 1st Matrix");
                for(int i=0; i<m1; i++)		//get input of 1st matrix
                {
                    for(int j=0; j<n1; j++)
                    {
                        System.out.print("Enter an element : ");
                        arr1[i][j]=sc.nextInt();
                    }
                }
                System.out.println("Inputting the 2nd Matrix");
                for(int i=0; i<m2; i++)		//get input of 2nd matrix
                {
                    for(int j=0; j<n2; j++)
                    {
                        System.out.print("Enter an element : ");
                        arr2[i][j]=sc.nextInt();
                    }
                }
                int s = 0;	// initially value of sum=0
                for(int i=0; i<m1; i++)	//multiplication of matrix start here
                {
                    for(int j=0; j<n2; j++)
                    {
                        for(int k=0; k<n1; k++)
                        {
                            s = s + arr1[i][k]*arr2[k][j];
                        }
                        mult[i][j]=s;
                        s=0;
                    }
                }
                 
                /* Print all the Matrix */
                System.out.println("          Output         ");
                System.out.println("The 1st Matrix is");
                ob.printMatrix(arr1,m1,n1);
                System.out.println("The 2nd Matrix is");
                ob.printMatrix(arr2,m2,n2);
                System.out.println("The Result of Multiplication is");
                ob.printMatrix(mult,m1,n2);
            }
        }
    }

    Output:

    Enter no. of rows of 1st Matrix : 3
    Enter no. of columns of 1st Matrix : 2
    Enter no. of rows of 2nd Matrix : 2
    Enter no. of columns of 2nd Matrix : 4

    Inputting the 1st Matrix

    Enter an element : 1
    Enter an element : 4
    Enter an element : 8
    Enter an element : 2
    Enter an element : 3
    Enter an element : 6

    Inputting the 2nd Matrix

    Enter an element : 5
    Enter an element : 2
    Enter an element : 3
    Enter an element : 1
    Enter an element : 4
    Enter an element : 5
    Enter an element : 6
    Enter an element : 8

    Output

    The 1st Matrix is
    1 4
    8 2
    3 6

    The 2nd Matrix is
    5 2 3 1
    4 5 6 8

    The Result of Matrix Multiplication is
    21 22 27 33
    48 26 36 24
    39 36 45 51

Also See:   program for display lower triangular matrix in java

 

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

Leave a Reply

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