C program to multiply two matrices

Program in the C programming language to find the product or multiplication of two matrices. Below is the C program to multiply two matrices –

```/* C program to multiply the two matrices */
#include <stdio.h>
#include<conio.h>
void main()
{
int m, n, p, q, c, d, k, sum = 0;
int ar1[10][10], ar2[10][10], mul[10][10];

printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for (  c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
scanf("%d", &ar1[c][d]);
}
}

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

if ( n != p )
{
printf("Multiplication is not possible of the two matrices \n");
}
else
{
printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < p ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
scanf("%d", &ar2[c][d]);
}
}

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + ar1[c][k]*ar2[k][d];
}

mul[c][d] = sum;
sum = 0;
}
}

printf("Product of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", mul[c][d]);

printf("\n");
}
}
getch();
}
```

Output –
Enter the number of rows and columns of first matrix – 2 2
Enter the elements of first matrix –
2 3
4 3
Enter the number of rows and columns of second matrix – 2 3
5 7 2
5 6 4
Product of entered matrices is –
25 32 16
35 46 20

9 thoughts on “C program to multiply two matrices”

1. Helena says:

I appreciate your posts on this and a simalir post on 3/24/2010.Here is my scenario, I have two columns. I need to add the first columns (the easy part). The second column is a percent that I need to subtract by 100 then multiply against \$1 for each line, then sum themHere is the input:942330863 96942172150 95942099452 92I need this as an output:2826602465 160169797I tried this code, but it’s just doing the percentage work and only doing it for the first line and not summing every line.awk ‘BEGIN {FS=”,”} NR == 1{ n1 = \$1; x = substr((100-\$2)/100,2,3); y = x*\$2; next } { n1 += \$1; y += \$y }END { printf (“%-15d%d\n”,n1,y) }’

2. Spero di riuscire ad andarlo a vedere. Lo Spiderman degli anni '70 l'avevo visto e forse proprio all'epoca inizia a collezionare fumetti che fino ad allora avevo solo letto saltuariamente.

3. OcchiGrazie Andrea, ho letto e l’ho trovato interessante. So che Paolo certe cose non puÃ² dirle chiaramente, si sconfina dai fatti nella politica. Viene quasi da pensare che popski e iltremendo abbiano ragione, i complottisti sono un complotto organizzato da Bush per passare sotto silenzio gli errori compiuti e la vulnerabilitÃ  degli USA. ARG! Sono diventato un complottista anch’io! 😉

4. Utrolig bra bilde …. og for en skjÃ¸nnhet! Jeg har noen som svermer rundt mine sÃ¸yleblomster, men som for deg, de flyr fort til neste blomst nÃ¥r jeg nÃ¦rmer meg med kameraet :o)

5. This info is the cat’s pajamas!

6. Visste inte att det hade kommit ett Ferrari-DLC till Need for Speed Shift, fÃ¥r vÃ¤l bara hoppas jag rÃ¥kar vara den utvalda att lyckas fÃ¥ denna kod :)//axelflensburg@hotmail.com

7. What an exciting year full of changes!And someone called you a running snob? YOU? Couldn’t be MORE wrong. What a horrible thing to say. Who are are they, I’ll go “tweet” them up! :o)Here’s to a fabulous 2011 – you are going to ROCK IT in typical Chic Runner style!