Matrix multiplication in java using function

Let’s learn matrix multiplication in java using function.

Matrix multiplication in java using function

matrix multiplication in java using function

Let’s learn java program to multiply two matrices by passing matrix to a function,

Also read – how to list all files in a directory in java

To multiply two matrix number of columns first matrix should be equal to number of rows of second matrix.

Here’s the matrix multiplication in java example program,

// matrix multiplication java
public class MatrixMultiplicationJavaDemo 
{
   public static int[][] multiplyMatrix(int matrix1[][], int matrix2[][], int row, 
int column, int col) 
   {
      int multiply[][] = new int[row][col];
      for(int a = 0; a < row; a++) 
      {
         for(int b = 0; b < col; b++) 
         {
            for(int k = 0; k < column; k++) 
            {
               multiply[a][b] += matrix1[a][k] * matrix2[k][b];
            }
         }
      }
      return multiply;
   }
   public static void printMatrix(int multiply[][]) 
   {
      System.out.println("Multiplication of two matrices : ");
      for(int[] row : multiply) 
      {
         for(int column : row) 
         {
            System.out.print(column + "    ");
         }
         System.out.println();
      }
   }

   public static void main(String[] args) 
   {
      int row = 2, col = 3;
      int column = 2;
      int matrixOne[][] = {{1, 2, 3}, {4, 5, 6}};
      int matrixTwo[][] = {{7, 8}, {9, 1}, {2, 3}};
      int product[][] = multiplyMatrix(matrixOne, matrixTwo, row, col, column);
      printMatrix(product);
   }
}




Output:

Multiplication of two matrices :
31 19
85 55

Matrix multiplication in java with scanner

Let’s learn matrix multiplication in java with scanner.

import java.util.Scanner;

public class MatrixMultiplicationScanner 
{
   public static void main(String[] args) 
   {
      int row1, col1, row2, col2, total = 0, a, b, c;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of rows and columns of first matrix: ");
      row1 = sc.nextInt();
      col1 = sc.nextInt();
      int[][] firstMatrix = new int[row1][col1];
      System.out.println("Please enter elements of first matrix: ");
      for(a = 0; a < row1; a++)
      {
         for(b = 0; b < col1; b++)
         {
            firstMatrix[a][b] = sc.nextInt();
         }
      }
      System.out.println("Please enter number of rows and columns of second matrix: ");
      row2 = sc.nextInt();
      col2 = sc.nextInt();
      if(col1 != row2)
         System.out.println("Matrices can't be multiplied");
      else
      {
         int[][] secondMatrix = new int[row2][col2];
         int[][] multiply = new int[row1][col2];
         System.out.println("Please enter elements of second matrix: ");
         for(a = 0; a < row2; a++)
            for(b = 0; b < col2; b++)
               secondMatrix[a][b] = sc.nextInt();
         for(a = 0; a < row1; a++) 
         {
            for(b = 0; b < col2; b++) 
            {
               for(c = 0; c < row2; c++)
                  total = total + firstMatrix[a][c] * secondMatrix[c][b];
               multiply[a][b] = total;
               total = 0;
            }
         }
         System.out.println("Matrix multiplication: ");
         for(a = 0; a < row1; a++) 
         {
            for(b = 0; b < col2; b++)
               System.out.print(multiply[a][b] + "\t");
            System.out.print("\n");
         }
      }
      sc.close();
   }
}


Output:

Please enter number of rows and columns of first matrix:
3
3
Please enter elements of first matrix:
2 4 6
8 1 2
3 4 5
Please enter number of rows and columns of second matrix:
3
3
Please enter elements of second matrix:
1 3 5
7 9 1
2 3 4
Matrix multiplication:
42 60 38
19 39 49
41 60 39


2×2 matrix multiplication in java

Now let’s learn 2×2 matrix multiplication in java example program,

public class Java2X2MatrixMultiplication
{
   public static void main(String[] args) 
   {
      int[][] matrix1 = {{2,4},{6,8}};
      int[][] matrix2 = {{1,3},{5,7}};
      int[][] multiply = new int[2][2];    
      for(int a = 0; a < 2; a++)
      {
         for(int b = 0; b < 2; b++)
         {
            multiply[a][b] = 0;      
            for(int c = 0; c < 2; c++)      
            {   
               multiply[a][b] += matrix1[a][c] * matrix2[c][b];      
            }  
            System.out.print(multiply[a][b] + " ");  
         }
         System.out.println();    
      } 
   }
}


Output:

22 34
46 74

Matrix multiplication in java without scanner

In the previous example we learnt matrix multiplication in java with scanner.

Now let’s learn matrix multiplication in java without scanner. Let’s see matrix multiplication in java example program,

public class MatrixMultiplicationWithoutScanner 
{
   public static void main(String[] args) 
   {
      int[][] mat1 = {{1,3,5},{7,9,2},{4,6,8}};
      int[][] mat2 = {{1,2,3},{4,5,6},{7,8,9}}; 
      int[][] multiply = new int[3][3];    
      for(int a = 0; a < 3; a++)
      {    
         for(int b = 0; b < 3; b++)
         {
            multiply[a][b] = 0;      
            for(int c = 0; c < 3; c++)      
            {
               multiply[a][b] += mat1[a][c] * mat2[c][b];      
            }
            System.out.print(multiply[a][b] + " ");  
         }  
         System.out.println();    
      }
   }
}


Output:

48 57 66
57 75 93
84 102 120

Also read – matrix addition in java using function