# C program to find multiplication of two matrices

In this C program, we are going to read two matrices and find its multiplication in another matrix.

Given (read) two matrices with r1, c1 and r2, c2 number of rows and columns and find its multiplication.

Here, we are created two user defined functions,

1. readMatrix - this will read matrix of given row and col (number of rows and columns)
2. printMatrix - this will print the element of given matrix, number of rows and cols are given.

## Program to multiply two matrices in C language

```#include <stdio.h>

#define MAXROW      10
#define MAXCOL      10

/*User Define Function to Read Matrix*/
{
int i,j;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
printf("Enter element [%d,%d] : ",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
}

/*User Define Function to Read Matrix*/
void printMatrix(int m[][MAXCOL],int row,int col)
{
int i,j;
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
printf("%d\t",m[i][j]);
}
printf("\n");
}
}

int main()
{
int a[MAXROW][MAXCOL],b[MAXROW][MAXCOL],result[MAXROW][MAXCOL];
int i,j,r1,c1,r2,c2;
int sum,k;

printf("Enter number of Rows of matrix a: ");
scanf("%d",&r1);
printf("Enter number of Cols of matrix a: ");
scanf("%d",&c1);

printf("\nEnter elements of matrix a: \n");

printf("Enter number of Rows of matrix b: ");
scanf("%d",&r2);
printf("Enter number of Cols of matrix b: ");
scanf("%d",&c2);

printf("\nEnter elements of matrix b: \n");

if(r1==c2)
{
/*Multiplication of two matrices*/
for(i=0;i< r1;i++)
{
for(j=0;j< c1;j++)
{
sum=0;
for(k=0;k< r1;k++)
{
sum=sum + (a[i][k]*b[k][j]);
}
result[i][j]=sum;
}
}

/*print matrix*/
printf("\nMatrix after multiplying elements (result matrix):\n");
printMatrix(result,r1,c1);

}
else
{
printf("\nMultiplication can not be done.");
}

return 0;
}
```

Output

```Enter number of Rows of matrix a: 3
Enter number of Cols of matrix a: 3

Enter elements of matrix a:
Enter element [1,1] : 1
Enter element [1,2] : 2
Enter element [1,3] : 3
Enter element [2,1] : 4
Enter element [2,2] : 5
Enter element [2,3] : 6
Enter element [3,1] : 7
Enter element [3,2] : 8
Enter element [3,3] : 9

Enter number of Rows of matrix b: 3
Enter number of Cols of matrix b: 3

Enter elements of matrix b:
Enter element [1,1] : 1
Enter element [1,2] : 1
Enter element [1,3] : 1
Enter element [2,1] : 2
Enter element [2,2] : 2
Enter element [2,3] : 2
Enter element [3,1] : 3
Enter element [3,2] : 3
Enter element [3,3] : 3

Matrix after multiplying elements (result matrix):
14	14	14
32	32	32
50	50	50
```

Preparation

What's New

Top Interview Coding Problems/Challenges!