Write a C Program Program For Multiplication of two matrices

Admin

 

Write a C Program Program For Multiplication of two matrices

#include<stdio.h>
#define ROW1 3
#define COL1 4
#define ROW2 COL1
#define COL2 2
int main(void)
{
	int mat1[ROW1][COL1],mat2[ROW2][COL2],mat3[ROW1][COL2];
	int i,j,k;
	printf("Enter matrix mat1(%dx%d)row-wise :\n",ROW1,COL1);
	for(i=0; i<ROW1; i++)
		for(j=0; j<COL1; j++)
			scanf("%d",&mat1[i][j]);
	printf("Enter matrix mat2(%dx%d)row-wise :\n",ROW2,COL2);
	for(i=0; i<ROW2; i++)
		for(j=0; j<COL2; j++)
			scanf("%d",&mat2[i][j] );
	/*Multiplication*/
	for(i=0; i<ROW1; i++)
		for(j=0; j<COL2; j++)
		{
			mat3[i][j] = 0;
			for(k=0; k<COL1; k++)
				mat3[i][j] += mat1[i][k] * mat2[k][j];
		}
	printf("The Resultant matrix mat3 is :\n");
	for(i=0; i<ROW1; i++)
	{
		for(j=0; j<COL2; j++)
			printf("%5d",mat3[i][j]);
		printf("\n");
	}
	return 0;
}

Output
Enter matrix mat1(3x4)row-wise :
   36   38   40   42
   44   46   48   50
   52   54   56   58
Enter matrix mat2(4x2)row-wise :
12 13
14 15
16 17 
18 19
The Resultant matrix mat3 is :
 2360 2516
 2840 3028
 3320 3540

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.