C Program to Multiply two Matrices

Matrix is an important chapter in the Mathematics that we study in 9th Grade. Multiplying Matrices is a complex process to do. In this C Programming example, we will write a C program to multiply two Matrices.

To begin with, you must have knowledge of the following concepts:

• Array
• For loop
• Matrices [Mathematics]

C Program to Multiply two Matrices

Here’s the C Program to multiply two Matrices:

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

int main()
{
int mat1[3][3],mat2[3][3],pd[3][3],i,j,k;
printf("Enter the elements of first matrix : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("Enter the elements or second matrix : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&mat2[i][j]);
}
}

printf("Multiplication of Matrix : \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
pd[i][j]=0;
for(k=0;k<3;k++)
{
pd[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",pd[i][j]);
}
printf("\n");
}
return 0;
}``````

Output

Here’s the output of the above C Program to Multiply two Matrices example: