This is a C Program to check multiplicability of two matrices.
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.
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
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.
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.
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.
/* C Program to check multiplicability of two matrices */
#include<stdio.h>
int main()
{
int m, n;
int p, q;
printf("Enter the dimensions of first matrix: ");
scanf("%d%d", &m, &n);
printf("Enter the dimensions of second matrix: ");
scanf("%d%d", &p, &q);
if( n != p )
{
printf("Two matrices CANNOT be multiplied !!!\n");
}
else
{
printf("Two matrices meet the criteria for Multiplication !!!\n");
printf("Dimensions of resultant matrix are: %dX%d", m,q);
}
return 0;
}
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.
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.
- Check Computer Science Books
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Check C Books
- Apply for Computer Science Internship