# C program to multiply two matrices

Here is the program for matrix multiplication in C.

m and n are rows and columns of the first matrix.

p and q are rows and columns of the second matrix.

Then, multiplication is possible only if n==p.

Program

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

int main()
{
int a,b,c,m,n,p,q,i,j,k;
printf("Enter rows and columns of first matrix:");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second matrix:");
scanf("%d%d",&p,&q);

if(n==p)
{
printf("\nEnter first matrix:\n");

for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);

printf("\nEnter second matrix:\n");

for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);

printf("\nThe new matrix is:\n");

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

printf("\n");
}
}
else
printf("\nSorry!!!! Matrix multiplication can't be done");

return 0;
}``````

Output

``````Enter rows and columns of first matrix:3
3
Enter rows and columns of second matrix:3
3

Enter first matrix:
1 2 3
4 5 6
7 8 9

Enter second matrix:
9 8 7
6 5 4
3 2 1

The new matrix is:
30 24 18
84 69 54
138 114 90``````

Remark beneath in the event that you have any questions identified with above program for matrix increase in C