**Matrix Multiplication in C – **If you are searching for How to do C program to multiply two matrices. Today, We are going to share step by step Guide to easily Matrix Multiplication in C. Following are all step by step Guide. Firstly user will enter the order of a matrix. If the entered orders of two matrix is such that they can’t be multiplied then an error message is displayed on the screen. You have already studied the logic to multiply them in Mathematics.

Read More – Pascal Triangle in C, C program to find fibonacci series

To multiply two matrices, the number of columns of first matrix should be equal to the number of rows to second matrix. This program displays the error until the number of columns of first matrix is equal to the number of rows of second matrix.

**Example:**

If matrix 1 =

1 2 3

4 5 6

7 8 9

And matrix 2 =

9 8 7

6 5 4

3 2 1

Product of both matrices =

30 24 18

84 69 54

138 114 90

#### Matrix Multiplication

Multiplication of matrices is different from simple Scalar multiplication of matrix in C. Two matrices can be multiplied only and only if number of columns in the first matrix is same as number of rows in second matrix.

**Multiplication of two matrices can be defined as**

## C program to multiply two matrices

`/**`

` `

`* C program to multiply two matrices`

` `

`*/`

`#include <stdio.h>`

`int`

`main()`

`{`

` `

`int`

`A[3][3], B[3][3], C[3][3];`

` `

`int`

`row, col, i, sum;`

` `

`/*`

` `

`* Reads elements in first matrix from user`

` `

`*/`

` `

`printf`

`(`

`"Enter elements in matrix A of size 3x3: \n"`

`);`

` `

`for`

`(row=0; row<3; row++)`

` `

`{`

` `

`for`

`(col=0; col<3; col++)`

` `

`{`

` `

`scanf`

`(`

`"%d"`

`, &A[row][col]);`

` `

`}`

` `

`}`

` `

`/*`

` `

`* Reads elements in second matrix from user`

` `

`*/`

` `

`printf`

`(`

`"\nEnter elements in matrix B of size 3x3: \n"`

`);`

` `

`for`

`(row=0; row<3; row++)`

` `

`{`

` `

`for`

`(col=0; col<3; col++)`

` `

`{`

` `

`scanf`

`(`

`"%d"`

`, &B[row][col]);`

` `

`}`

` `

`}`

` `

`/*`

` `

`* Multiplies both matrices A*B`

` `

`*/`

` `

`for`

`(row=0; row<3; row++)`

` `

`{`

` `

`for`

`(col=0; col<3; col++)`

` `

`{`

` `

`sum = 0;`

` `

`/*`

` `

`* Multiplies row of first matrix to column of second matrix`

` `

`* And stores the sum of product of elements in sum.`

` `

`*/`

` `

`for`

`(i=0; i<3; i++)`

` `

`{`

` `

`sum += A[row][i] * B[i][col];`

` `

`}`

` `

`C[row][col] = sum;`

` `

`}`

` `

`}`

` `

`/*`

` `

`* Prints the product of matrices`

` `

`*/`

` `

`printf`

`(`

`"\nProduct of Matrix A * B = \n"`

`);`

` `

`for`

`(row=0; row<3; row++)`

` `

`{`

` `

`for`

`(col=0; col<3; col++)`

` `

`{`

` `

`printf`

`(`

`"%d "`

`, C[row][col]);`

` `

`}`

` `

`printf`

`(`

`"\n"`

`);`

` `

`}`

` `

`return`

`0;`

`} `

## Leave a Reply