img
Question:
Published on: 21 December, 2024

Write a C program to multiplication of two matrix.

Answer:

 

#include<stdio.h>
#include<conio.h>
main()
{
	int a[10][10],b[10][10],c[10][10],m,n,p,q,i,j,k;
	printf("\n Enter the dimension of 1st matrix");
	scanf("%d %d",&m,&n);
	printf("\n Enter the dimension of 2nd matrix");
	scanf("%d %d",&p,&q);
	if(n==p)
	{
		printf("\n the multiplication is possiable.");
		printf("\n Enter the element of 1st matrix");
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			scanf("%d",&a[i][j]);
		}
		printf("\n Enter the element of 2nd matrix");
		for(i=0;i<p;i++)
		{
			for(j=0;j<q;j++)
			scanf("%d",&b[i][j]);
		}
		for(i=0;i<m;i++)
		{
			for(j=0;j<q;j++)
			{
				c[i][j]=0;
				for(k=0;k<n;k++)
				c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
			}
		}
		printf("\n The 1st input matrix is\n");
		for(i=0;i<m;i++)
		{
			for(j=0;j<n;j++)
			{
				printf("\t%d",a[i][j]);
			}
			printf("\n");
		}
		printf("\n The 2nd input matrix is\n");
		for(i=0;i<p;i++)
		{
			for(j=0;j<q;j++)
			{
				printf("\t%d",b[i][j]);
			}
			printf("\n");
		}
		printf("\n After multiplication the result matrix is\n");
		for(i=0;i<m;i++)
		{
			for(j=0;j<q;j++)
			{
				printf("\t%d",c[i][j]);
			}
			printf("\n");
		}
	}
	else
	 printf("\n The multiplication is not possable");
	getch(); 
}

Output:

Random questions