C Program to Check Multiplicability of Two Matrices

This is a C Program to check multiplicability of two matrices.

Problem Description

We have to write a C program to check whether the two matrices can be multiplied of not. We have to input the dimensions of two matrices i.e. number of rows and columns in each matrix and calculate whether they can be multiplied or not.

Expected Input and Output

Case 1. When we can multiply two matrices

If the matrix 1 has dimensions as:
Rows (m) = 4
Columns (n) = 2
and matrix 2 has dimensions as:
Rows (m) = 2
Columns (n) = 3

Output: Matrices can be multiplied. Resultant matrix will have dimensions m = 4, and n = 3.

Case 2. When we cannot multiply two matrices

advertisement
advertisement
If the matrix 1 has dimensions as:
Rows (m) = 3
Columns (n) = 3
and matrix 2 has dimensions as:
Rows (m) = 2
Columns (n) = 3

Output: Matrices cannot be multiplied.

Problem Solution

1. In order to multiply two matrices, the number of columns in first matrix must be equal to the number of rows in other matrix.
2. For matrix 1 number of rows (say m) = 4 and number of columns (say n) = 2, and for matrix 2, m = 2 and n = 3 then we can multiply both matrices and the resulting matrix will have dimensions as: m = 4 and n = 3.

Program/Source Code

Here is source code of the C Program checking whether we can multiply two matrices or not. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on windows 10. The program output is also shown below.

  1. /* C Program to check multiplicability of two matrices */
  2. #include<stdio.h>
  3. int main()
  4. {
  5.     int m, n;
  6.     int p, q;
  7.     printf("Enter the dimensions of first matrix: ");
  8.     scanf("%d%d", &m, &n);
  9.     printf("Enter the dimensions of second matrix: ");
  10.     scanf("%d%d", &p, &q);
  11.     if( n != p )
  12.     {
  13.         printf("Two matrices CANNOT be multiplied !!!\n");
  14.     }
  15.     else
  16.     {
  17.         printf("Two matrices meet the criteria for Multiplication !!!\n");
  18.         printf("Dimensions of resultant matrix are: %dX%d", m,q);
  19.     }
  20.     return 0;
  21. }
Program Explanation

1. In the above program we have just checked the condition for multiplication of two matrices i.e. the number of columns in first matrix should be equal to number of rows in the other.
2. If the columns in first matrix i.e (n) is equal to number of rows in second matrix i.e. (p), then matrices can be multiplied and the resultant matrix will have dimensions as mXq.

advertisement
Runtime Test Cases
1. Enter the dimensions of first matrix: 4 2
   Enter the dimensions of second matrix: 2 3
   Two matrices meet the criteria for Multiplication !!!
   Dimensions of resultant matrix are: 4X3
 
2. Enter the dimensions of first matrix: 3 3
   Enter the dimensions of second matrix: 2 3
   Two matrices CANNOT be multiplied !!!

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

advertisement

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.