C Program for Matrix Multiplication or Matrix Multiplication in C Example Source Code

C Program for Matrix Multiplication or Matrix Multiplication in C Example Source Code given below:

Matrix Multiplication Example 1 :-

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
clrscr(); // first clear the screen

printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if ( n != p )
{
printf("Sorry! Matrix Multiplication can't be done, study row col combination of Matrices...\n");
getch(); // uses wait comand and in conio.h
exit(0); // to exit program and in stdlib.h
}
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&first[c][d]);
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d",&second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t",multiply[c][d]);
printf("\n");
}

getch();
return 0;
}

Matrix Multiplication Example 2 ( a few codes used here ) :-

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
clrscr();

printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d",&p,&q);
if ( n != p )
{
printf("Sorry! Matrix Multiplication can't be done, study row col combination of Matrices...\n");
getch(); // uses wait comand and in conio.h
exit(0); // to exit program and in stdlib.h
}
printf("Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d",&first[c][d]);
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d",&second[c][d]);

for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; multiply[c][d] = sum, sum = 0, d++ )
for ( k = 0 ; k < p ; sum = sum + first[c][k]*second[k][d], k++ ); /* for loop doesn't have other statements, so use ";" to end loop */
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; printf("\n"), c++ )
for ( d = 0 ; d < q ; printf("%d\t",multiply[c][d]), d++ ); /* for loop doesn't have other statements, so use ";" to end loop */
getch();
return 0;
}