# Matrix Multiplication Program in C In this article, we will discuss the matrix multiplication program in C. We will discuss how to multiply 2 matrices, the multiplication criteria of matrices, and then the matrix multiplication program in C. So, let’s get started.

## Matrix Multiplication Criteria

Matrix multiplication is more complex than matrix addition. Consider the 2 matrices being added as shown below So, the matrix addition is pretty simple. If both matrices have the same number of rows and columns, they can be added. This is called the matrix addition rule or matrix addition criteria. They are added by adding their corresponding values.

However, the criteria to multiply 2 matrices is not that simple. Consider the 2 matrices shown below. So, we have a matrix of size 2×3 i.e. 2 rows and 3 columns and we have another matrix of size 3×3 i.e. 3 rows and 2 columns. These 2 matrices cannot be added as they don’t have the same number of rows and columns. But, what if we say that these matrices can be multiplied?

Yes, in fact, these matrices can be multiplied. The matrix multiplication criteria or the rule to multiply 2 matrices is as follows.

For 2 matrices to be multiplied, the number of columns of the first matrix must be equal to the number of rows of the second matrix, and the resultant matrix will be of the order r1xc2 where r1 is the number of rows of the first matrix and c2 is the number of columns of the second matrix. So, we have understood the matrix multiplication criteria. However, we still need to understand the method to multiply the matrices. So, let us now study the method to multiply matrices.

## Matrix Multiplication

Consider the matrices shown below. So, since the number of columns of the first matrix is equal to the number of rows of the second matrix, these 2 matrices can be multiplied and the resultant will be of the size row1 x col2 = 2 x 3.

Now, the question is how to get the answer to multiplication.

For calculating each element of the resultant matrix, we need to multiply the corresponding rows and columns of the 2 input matrices. For instance, if we need to find the element a12, we will take the first row of the first matrix and the second column of the second matrix and will multiply their corresponding elements.

If we take a23, we will take the second row of the first matrix and the third column of the second matrix, and multiply their corresponding elements.

Let us multiply the 2 matrices shown below. The step-by-step multiplication for the calculation of each element of the resultant matrix is as follows.

So, to get the element a11, we will multiply the first row of the first matrix with the first column of the second matrix as shown below. Now, we will calculate the next element i.e. a12. For that, we will multiply the first row of the first matrix with the second column of the second matrix as shown below. Now, we will calculate a13 by multiplying the first row of the first matrix with the third column of the second matrix as shown below. Now, we will find out the value of a21. For that, we multiply the second row of the first matrix with the first column of the second matrix as shown below. Now, to calculate the value of a22, we will multiply the second row of the first matrix with the second column of the second matrix as shown below. Finally, to calculate the value of a23, we will multiply the second row of the first matrix by the third column of the second matrix. So, this is how matrix multiplication takes place. So, now that we have understood the complete procedure of matrix multiplication, let us now write the matrix multiplication program in C language.

## C Program of Matrix Multiplication

The matrix multiplication program in C is shown below.

```# include <stdio.h>
# include <stdlib.h>

void input(int arr[], int m, int n) {
int i, j;
printf("\nEnter elements of matrix:\n");

for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf("Enter elements a%d%d: ", i + 1, j + 1);
scanf("%d", & arr[i][j]);
}
}
}

void display(int arr[], int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d", arr[i][j]);
}
}
}

void multiply(int a[], int b[], int c[], int m, int n, int p, int q) {
int i, j, k;
for (i = 0; i < m; ++i) {
for (j = 0; j < q; ++j) {
c[i][j] = 0;
}
}

for (i = 0; i < m; ++i) {
for (j = 0; j < q; ++j) {
for (k = 0; k < n; ++k) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}

int main(){
int m, n, p, q, i, j, k;
int a, b, res;

printf("Enter the no of rows and cols of first matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the number of rows and cols of the second matrix\n");
scanf("%d%d", & p, & q);

if (n != p) {
printf("Matrix is incompatible for multiplication\n");
} else {
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}

printf("Enter the elements of Matrix-B:\n");
for (i = 0; i < p; i++) {
for (j = 0; j < q; j++) {
scanf("%d", & b[i][j]);
}
}

for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
res[i][j] = 0;
for (k = 0; k < p; k++) {
res[i][j] += a[i][k] * b[k][j];
}
}
}

printf("The product of the two matrices is:-\n");

for (i = 0; i < m; i++) {
for (j = 0; j < q; j++) {
printf("%d\t", res[i][j]);
}
printf("\n");
}
}

return 0;
}```

## Matrix Multiplication Algorithm:

So, we have to replicate the procedure studied above in the code. The algorithm for writing a matrix multiplication program in C is as follows.

1. Take r1 and c1 input from the user.
2. Take r1xc1 elements as input from the user. These are the elements of the first matrix.
3. Take r2 and c2 input from the user.
4. Take r2xc2 elements as input from the user. These are the elements of the second matrix.
5. Now check for matrix multiplication criteria i.e. check if c1 equals r2. If they are not equal, print that the matrix multiplication is not possible and exit from the program.
6. If they are equal, perform matrix multiplication by multiplying each column with the corresponding row to get the corresponding row elements of the resultant matrix.
7. Print the resultant matrix.

## Time Complexity of the Matrix Multiplication Program in C:

The time complexity of the matrix multiplication program in C is O(n3) for a square matrix or O(m x n x k) for rectangular matrices where n is the dimension of the square matrix, m is the number of rows of the first matrix and n is the number of columns of the second matrix and k is the common element i.e. the number of columns of the first matrix or the number of rows of the second matrix.

## Space Complexity of the Matrix Multiplication Program in C:

There is no auxiliary space used to multiply the matrices. So, the auxiliary space is O(1). However, we have multiplied the matrices and stored the result in another matrix. So, the output space is O(n2) for the square matrix and O(m x n) for rectangular matrices where m is the number of rows of the first matrix and n is the number of columns of the second matrix.

So, this is how we multiply matrices and write a matrix multiplication program in C. We hope that you have understood the discussion completely and have liked the article. We hope to see you again soon at PrepBytes.