Matrix multiplication in Java

Source code :

public class MatrixMultiplication

{

    public static void main(String args[])

    {
        int i,j,k;
       int a[][] =new int[2][2];
       int b[][] = new int [2][2];
       int c[][] = new int [2][2];
	   
        a[0][0]=10;  
        a[0][1]=2;  
        a[1][0]=3;  
        a[1][1]=4;  
      
        b[0][0]=2;  
        b[0][1]=3;  
        b[1][0]=4; 
        b[1][1]=5; 
       
        System.out.println("Multiplying the matrices...");

        for ( i = 0; i < 2; i++)

        {

            for ( j = 0; j < 2; j++)

            {

                for ( k = 0; k < 2; k++)

                {

                    c[i][j] = c[i][j] + a[i][k] * b[k][j];

                }

            }

        }

        System.out.println("The product is:");

        for ( i = 0; i < 2; i++)

        {

            for ( j = 0; j < 2; j++)

            {

                System.out.print(c[i][j] + " ");

            }

            System.out.println();

        }

    

    }

}

Output :

Multiplying the matrices...
The product is:
28 40
22 29 

Algorithm :

  1. Initialize the variables as i,j and k
  2. Enter the values of matrix 1 and matrix 2 in the program
  3. Use the formula for multiply the matrices
  4. Compile the java program
  5. Execute the file

Output :


                

Comments :