Using - C
// Developed by Anuj Das
#include<stdio.h>
#include<stdlib.h>
void main() {
int a[10][10], b[10][10], c[10][10];
int rows_m1, cols_m1, rows_m2, cols_m2, i, j, k;
printf("Enter number of Rows & Columns of 1st Matrix: ");
scanf("%d %d", &rows_m1, &cols_m1);
printf("Enter number of Rows & Columns of 2nd Matrix: ");
scanf("%d %d", &rows_m2, &cols_m2);
if(cols_m1 != rows_m2) {
printf("\nNumber of Columns of the 1st Matrix must be equal to the number of Rows of the 2nd Matrix!!");
exit(1);
}
printf("Enter Elements of 1st Matrix: \n");
for(i=0; i < rows_m1; i++) {
for(j=0; j < cols_m1; j++) {
scanf("%d",&a[i][j]);
}
}
printf("Enter Elements of 2nd Matrix: \n");
for(i=0; i < rows_m2; i++) {
for(j=0; j < cols_m2; j++) {
scanf("%d",&b[i][j]);
}
}
for(i=0; i < rows_m1; i++) {
for(j=0; j < cols_m2; j++) {
c[i][j]=0;
for(k=0; k < rows_m2; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("\n\nPRODUCT OF TWO MATRICES:\n");
for(i=0; i < rows_m1; i++) {
for(j=0; j < cols_m2; j++) {
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
OUTPUT
Enter number of Rows & Columns of 1st Matrix : 2 2
Enter number of Rows & Columns of 2nd Matrix: 2 3
Enter Elements of 1st Matrix:
1 2
2 3
Enter Elements of 2nd Matrix:
2 4 6
5 7 2
PRODUCT OF TWO MATRICES:
12 18 10
19 29 18
Using - C++
// Developed by Anuj Das
#include<iostream>
using namespace std;
int main() {
int a[10][10], b[10][10], c[10][10];
int rows_m1, cols_m1, rows_m2, cols_m2, i, j, k;
cout<< "Enter number of Rows & Columns of 1st Matrix: ";
cin>> rows_m1 >> cols_m1;
cout<< "Enter number of Rows & Columns of 2nd Matrix: ";
cin>> rows_m2 >> cols_m2;
if(cols_m1 != rows_m2) {
cout<< "\nNumber of Columns of the 1st Matrix must be equal to the number of Rows of the 2nd Matrix!!";
exit(1);
}
cout<< "Enter Elements of 1st Matrix: \n";
for(i=0; i < rows_m1; i++) {
for(j=0; j < cols_m1; j++) {
cin>> a[i][j];
}
}
cout<< "Enter Elements of 2nd Matrix: \n";
for(i=0; i < rows_m2; i++) {
for(j=0; j < cols_m2; j++) {
cin>> b[i][j];
}
}
for(i=0; i < rows_m1; i++) {
for(j=0; j < cols_m2; j++) {
c[i][j]=0;
for(k=0; k < rows_m2; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
cout<< "\n\nPRODUCT OF TWO MATRICES:\n";
for(i=0; i < rows_m1; i++) {
for(j=0; j < cols_m2; j++) {
cout<< c[i][j]<< "\t";
}
cout<< endl;
}
return 0;
}
OUTPUT
Enter number of Rows & Columns of 1st Matrix : 3 2
Enter number of Rows & Columns of 2nd Matrix: 3 2
Number of Columns of the 1st Matrix must be equal to the number of Rows of the 2nd Matrix!!