matrix multiplication program in c
C Programming

Matrix Multiplication Program in C

Matrix Multiplication Program in C

  • Matrix Multiplication Program in C is an important program for the C Programming Subject.
  • Matrix Multiplication in C Program is generally asked in AKTU Examination.
  • In this tutorial we have explained the Matrix Multiplication in C Program with implementation and output.
  • This matrix multiplication with example program will be definitely helpful for students.

Matrix Multiplication Program Algorithm

Necessary condition to perform the multiplication of two matrix is that number of column in first matrix should be equal to number of rows in second matrix.

It means in our matrix multiplication program in c we have to enter same value for number of column for first matrix and number of rows in second matrix.

Algorithm to implement the matrix multiplication with example in C is discussed in this section step wise step.

Step 1 – Declare Three two dimensional arrays. Two for input matrices A , B and C third to store the result of multiplication.

Step 2 – Input the number of rows and columns

Step 3 – Enter the elements of First and second Matrix.

Step 4- Use Three nested loops to perform multiplication . Outer most loop for row, first inner loop for column and second inner loop or inner most loop corresponding to multiplication of each row of first matrix with column of second matrix.

Step 5- Print the resultant matrix which store the result of multiplication.

C Program for matrix multiplication is given here

#include<stdio.h>

void main()

{

int a[10][10], b[10][10], mul[10][10],r,c,i,j,k;

printf(“enter the number of row=”);

scanf(“%d”,&r);

printf(“enter the number of column=”);

scanf(“%d”,&c);

printf(“enter the first matrix element=\n”);

for(i=0;i<r;i++)
{

for(j=0;j<c;j++)
{

scanf(“%d”,&a[i][j]);

}

}

printf(“enter the second matrix element=\n”);

for(i=0;i<r;i++)
{

for(j=0;j<c;j++)

{

scanf(“%d”,&b[i][j]);

}

}

printf(“multiply of the matrix=\n”);

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{

mul[i][j]=0;

for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}

//for printing result

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf(“%d\t”,mul[i][j]);
}
printf(“\n”);
}
}

Output

matrix multiplication in c

 

In the above matrix multiplication program in C,  variable r and c are used for number of rows and number of columns.

Conclusion and Summary

In this tutorial we have discussed the algorithm and program to perform the multiplication of two matrices in C.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *