C Program to multiply two matrices

The following is a C program to multiply two matrices:

Expected Output:

How it works

Two matrices can be multiplied only if the number of columns in the first matrix is equal to the number of rows in the second matrix.

Let, A be the matrix of size 2x3 and B be the matrix of size 3x2, Then, A * B is given by:

\left(\begin{array}{ccc}a & b & c \\d & e & f\end{array}\right) * \left(\begin{array}{cc}g & h\\k &l \\o & p \end{array}\right) = \left(\begin{array}{cc}a*g + b * k + c * o &a*h + b * l + c * p \\d*g + e * k + f * o &d*h + e * l + f * p\end{array}\right)

In general, If matrix A is of size m x n and B is of size n x p, then the size of matrix A * B will be m x p.

Here is how the program works:

  1. The first for loop (lines 20-26) asks the user to input the first matrix.
  2. The second for loop (lines 31-37) asks the user to input the second matrix.
  3. The third for loop (lines 42-55) multiplies the matrices.
  4. The fourth for loop (lines 58-65) prints the result of matrix multiplication.

Recommended Reading:

Leave a Comment

%d bloggers like this: