top of page
Writer's pictureanitadevkar

C Program to multiply 2 matrices

Write a C program to accept details of two matrices, Multiply given matrices and print the result.

#include <stdio.h>

int main()

{

int a[10][10],b[10][10],c[10][10],mm[10][10],i,j,k,m,n;

printf("Enter the value of row size ");

scanf("%d",&m);

printf("Enter the value of col size ");

scanf("%d",&n);

printf("Enter first matrix\n"); //Accepting first matrix from user

for(i=0;i<m;i++)

{

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

{

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

}

printf("\n");

}

printf("Enter Second matrix\n"); //Accepting second matrix from user

for(i=0;i<m;i++)

{

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

{

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

}

printf("\n");

}

for(i=0;i<m;i++) //multiplication in third matrix

{

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

{

mm[i][j]=0; //intitialization to 0

for(k=0;k<m;k++)

{

mm[i][j] =mm[i][j]+a[i][k]*b[k][j]; //matrix multiplication

}

}

printf("\n");

}

printf("Multiplication matrix is: \n");

for(i=0;i<m;i++)

{

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

{

printf("%d\t",mm[i][j]);

}

printf("\n");

}

return 0;

}

Output: Testcase-1

Enter the value of row size 2

Enter the value of col size 2

Enter first matrix

3 4


6 7


Enter Second matrix

1 2


3 4


Multiplication matrix is:

15 22

27 40

5 views0 comments

Recent Posts

See All

C Program for pyramid of number

Program: #include <stdio.h> int main() { int i,j; for(i=5;i>0;i--) //for rows { for(j=1;j<=i;j++) //for columns j loop will iterate...

Program to print Triangle

Program: #include <stdio.h> int main() { int i,j; for(i=5;i>0;i--) //for rows { for(j=0;j<i;j++) //for columns j loop will iterate...

Comments


bottom of page