Matrix Multiplication in Java

Matrix Multiplication Condition

For performing matrix multiplication, no the column in the first matrix should be equal to the no of rows in the second matrix. If this condition is not satisfied then we can’t perform matrix multiplication.

Java code for Matrix Multiplication

Steps for Java Multiplication Code

  • Take both matrix input from user
  • check if the matrix multiplication condition is satisfied or not, if the condition is not satisfied then exit from the program
  • else
  • multiply both matrices using 2 for using 3 for loop (check in the code)
  • print the result matrix

Matrix multiplication Java Code

matrix_multiplication.java

import java.util.*;
public class MatrixMultiplication {

        public static void main(String[] args) {

            Scanner sc = new Scanner(System.in);

            System.out.println("Enter the number of row in matrix1");
            int n1 = sc.nextInt();

            System.out.println("Enter the number of column in matrix1");
            int m1 = sc.nextInt();

            System.out.println("Enter the number of row in matrix2");
            int n2 = sc.nextInt();

            System.out.println("Enter the number of column in matrix2");
            int m2 = sc.nextInt();

            // checking matrix multiplication condition
            if(m1!=n2) {
                System.out.println(" Matrix multiplication is not possible");

            }
            else {

                int[][] matrix1 = new int[n1][m1];
                int[][] matrix2 = new int[n2][m2];

                System.out.println("Enter the matrix1");
                for(int i=0;i<n1;i++) {
                    for(int j=0;j<m1;j++) {
                        matrix1[i][j] = sc.nextInt();
                    }
                }


                System.out.println("Enter the matrix2");
                for(int i=0;i<n2;i++) {
                    for(int j=0;j<m2;j++) {
                        matrix2[i][j] = sc.nextInt();
                    }
                }

                // declaring 2d array for storing result of matrix multiplication

                int[][] multiplication = new int[n1][m2];

                // Performing matrix multiplication

                for(int i=0;i<n1;i++) {
                    for(int j=0;j<m2;j++) {
                        for(int k=0;k<n2;k++) {
                            multiplication[i][j] += matrix1[i][k]* matrix2[k][j];
                        }
                    }
                }

                // printing resultant matrix
                for(int i=0;i<n1;i++) {
                    for(int j=0;j<m2;j++) {
                        System.out.print(multiplication[i][j]+" ");
                    }
                    System.out.println();
                }
            }

        }
    }

you can enter any matrix of any dimension and perform matrix multiplication if the matrix multiplication condition is satisfied.

Similar Java Tutorials

Leave a Comment