C Program for Multiplying the two Matrices

CWC
1 Min Read

C Program for Multiplying the two Matrices

A prerequisite for multiplying two matrices is that the number of columns in the first matrix must be equal to the number of rows in the second matrix.

This program enters the size (rows and columns) of two matrices. To multiply two matrices, the number of columns of the first matrix should be equal to the number of rows of the second matrix.

Here is the source code of the C program to perform matrix multiplication. The C program is successfully compiled and run on a windows system

#include 
int main()
{
 int matA[2][3], matB[3][4], matR[2][4];
 int i,j,k;
 printf("Enter elements of the first matrix of order 2 x 3 \n");
 for(i=0;i<2;i++)
 {
 for(j=0;j<3;j++)
 {
 scanf("%d",&matA[i][j]);
 }
 }
 printf("Enter elements of the second matrix of order 3 x 4 \n");
 for(i=0;i<3;i++)
 {
 for(j=0;j<4;j++)
 {
scanf("%d",&matB[i][j]);
 }
 }
 for(i=0;i<2;i++)
 {
 for(j=0;j<4;j++)
 {
 matR[i][j]=0;
 for(k=0;k<3;k++)
 {
 matR[i][j]=matR[i][j]+matA[i][k]*matB[k][j];
 }
 }
 }
 printf("\nFirst Matrix is \n");
 for(i=0;i<2;i++)
 {
 for(j=0;j<3;j++)
 {
 printf("%d\t",matA[i][j]);
 }
 printf("\n");
 }
 printf("\nSecond Matrix is \n");
 for(i=0;i<3;i++)
 {
 for(j=0;j<4;j++)
 {
 printf("%d\t",matB[i][j]);
 }
 printf("\n");
 }
 printf("\nMatrix multiplication is \n");
 for(i=0;i<2;i++)
 {
 for(j=0;j<4;j++)
 {
 printf("%d\t",matR[i][j]);
 }
 printf("\n");
 }
 return 0;
}
Share This Article
Leave a comment

Leave a Reply

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

English
Exit mobile version