## Jul 21, 2020

### C program to print multiplication of two matrices using array

Ex: Write a C program to print multiplication of two matrices. How to write a C program to print multiplication of two matrices. C program to print multiplication of two matrices.

Input from user:

Enter elements in first matrix of size 3x3:
1 3 5
2 4 6
3 5 7

Enter elements in second matrix of size 3x3:
1 3 5
2 4 6
3 5 7

Expected output:

Multiplication of two matrices=
22 40 58
28 52 76
34 64 94

### Logic to multiply two matrices:

To print multiplication of two matrices we need to multiply it into below(row*col) format:

### Step by step logic of the given program:

1. Accept two matrices from user declare varible say a and b(use 2 dimensional array to store elements).

2. Store elements one by one in matrix a using:

for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &a[row][col]);
}
}
Follow same these  steps for matrix b.

3. Multiply two matrices a&b element wise using:

for(i=0;i<size;i++)
{
sum+=a[row][i]*b[i][col];

}

4. Print multiplication of two matrices on the output screen.

## Program to print multiplication of two matrices using array:

#include<stdio.h>
#define size 3//size of matrix

int main()
{
int a[size][size],
b[size][size],
c[size][size];

int row,col,i,sum=0;

//accept elements in first matrix
printf("Enter elements in first matrix of size 3x3:\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d",&a[row][col]);
}
}

//accept elements in second matrix
printf("Enter elements in second matrix of size 3x3:\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d",&b[row][col]);
}
}
//multiply both matrices a&b
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
for(i=0;i<size;i++)
{
sum+=a[row][i]*b[i][col];
}
c[row][col]=sum;
sum=0;
}
}

//print multiplication of matrices
printf("Multiplication of matrices=\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
printf("%d  ",c[row][col]);
}
printf("\n");
}
return 0;
}

Above program shows the following output:

Share your thoughts below in the comment section....