WAP to multiply two matrix

Write a program to multiply two matrix

#include<stdio.h>

int main() {
	int a[5][5],b[5][5],c[5][5],row,column,n,m,p,q,i,j,k;

	printf("Enter the size of the 1st matrix.\n");
	scanf("%d%d",&m,&n);

	printf("Enter the size of the 2nd matrix.\n");
	scanf("%d%d",&p,&q);

	if(n==p) {
		printf("Enter element in 1st matrix:\n");
		for(i=0;i<m;i++) {
			for(j=0;j<n;j++)
				scanf("%d",&a[i][j]);
		}

		printf("Enter element in 2nd matrix:\n");
		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]+=a[i][k]*b[k][j];
			}
		}

		for(i=0;i<m;i++) {
			for(j=0;j<q;j++)
				printf("%d ",c[i][j]);
			printf("\n");
		}
	}

	return 0;
}

Output

Enter the size of the 1st matrix.
2
2
Enter the size of the 2nd matrix.
2
2
Enter element in 1st matrix:
1
2
3
4
Enter element in 2nd matrix:
1
2
3
4
7 10
15 22

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments