Java Program to Check the Multiplicability of Two Matrices

This is the java program to check the multiplication of the two matrices. Two matrices are multiplicable if and only if the number of rows of the left hand side matrix is equal to the number of columns of the right hand side matrix.

Here is the source code of the Java Program to Check Multiplicability of Two Matrices. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is sample program to find out whether the two matrices are multiplicable or not
  2. //The complexity of the code is O(n)(linear)
  3. import java.util.Scanner;
  4.  
  5. public class Multiplicability_Matrix 
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         Scanner sc = new Scanner(System.in);
  10.         System.out.println("Enter the dimension of the matrix:\n ");
  11.         int rowA = sc.nextInt();
  12.         int colA = sc.nextInt();
  13.  
  14.         System.out.println("Enter the dimension of the other matrix:\n ");
  15.         int rowB = sc.nextInt();
  16.         int colB = sc.nextInt();
  17.  
  18.         if(colA == rowB)
  19.         {
  20.             System.out.println("Matrices are multipilcable");
  21.         }
  22.         else
  23.         {
  24.             System.out.println("Matrices are not multipilcable");
  25.         }
  26.         sc.close();
  27.  
  28.     }
  29. }

Output:

$ javac Muliplicability_Matrix.java
$ java Muliplicability_Matrix
Enter the dimension of the matrix:
1 2
Enter the dimension of the other matrix:
2 3
Matrices are multipilcable

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.