# C Program to Multiply to Matrix Using Multi-dimensional Arrays

This program asks user to enter two matrices and this program multiplies these two matrix and displays it. If you don't know matrix multiplication, visit this page to learn, how two matrix can be multiplied.

## Source code to multiply to matrix in C programming

```
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
}
/* Storing elements of first matrix. */
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Storing elements of second matrix. */
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
/* Initializing elements of matrix mult to 0.*/
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult. */
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
mult[i][j]+=a[i][k]*b[k][j];
}
/* Displaying the multiplication of two matrix. */
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ",mult[i][j]);
if(j==c2-1)
printf("\n\n");
}
return 0;
}
```

**Output**

In this program, user is asked to enter the size of two matrix at first. The column of first matrix should be equal to row of second matrix for multiplication. If this condition is not satisfied then, the size of matrix is again asked using while loop. Then, user is asked to enter two matrix and finally the output of two matrix is calculated and displayed.

This program is little bit larger and it is better to solve this program by passing it to a function. Visit this page to learn about multiplying matrices by passing arrays to a function.