# C Program to Multiply any Two Matrices

## C Program to Multiply Two Matrices

This page has a C Program to Multiply any Two Matrices. It is clear that, this C program will Multiply any Two Matrices. To multiply any two matrices, the number of columns of the first matrix must be equal to the number of rows  of the the second matrix. If the size of A matrix is (5 x 3), and the size of B matrix is (3 x 4), then the two matrices can be multiplied. If the size of A matrix is (4 x 3), and the size of B matrix is (4 x 2), then the two matrices can not be multiplied. For multiplying any two matrices, the two matrices must be compatible. The below given C program will find the product (Multiply) of any Two Matrices. Kindly check out the program to Multiply any Two Matrices. The Questions for the matrix multiplication program can be asked as . . .

1). C program to Multiply any Two n X n Matrices.

2). C program to find the product of any Two m X m Matrices.

3). C program to Multiply one matrix with other.

4). C program to find the product of any Two Matrices .

5). C program to Multiply any Two Matrices

6). C program to find the product of any Two Matrices

7). C program to Multiply one matrix with other on same size

8). C program to find the product of any Two square Matrices

9). C program to find the product of Two 3 X 3 Matrices

10). C program to find product of Two Matrices

11). C program to multiply Two 3 X 3 Matrices

12). C program to multiply Two Matrices

13). C program to print the product of Two Matrices

14). C program to display the product of Two Matrices

### Matrix multiplication Program

#include<stdio.h>
int main(){
int matrix1[10][10], matrix2[10][10], matrix[10][10];
int i, j, k, ms11, ms12, ms21, ms22;

clrscr();

printf("\n Enter the size of 1st Matrix :   \n");

scanf("%d %d", &ms11, &ms12);

printf("\n Enter the size of 2nd Matrix :   \n");

scanf("%d %d", &ms21, &ms22);

if(ms12 == ms21){
printf("\n Enter the elements of 1st Matrix : \n"); // Read 1st Matrix
for (i = 1; i <= ms11; i++){
for (j = 1; j <= ms12; j++){
scanf("%d", &matrix1[i][j]);
}
}
printf("\n Enter the elements of 2nd Matrix : \n"); // Read 2nd Matrix
for (i = 1; i <= ms21; i++){
for (j = 1; j <= ms22; j++){
scanf("%d", &matrix2[i][j]);
}
}
printf("\n The 1st Matrix is : \n\n"); // Print 1st Matrix
for (i = 1; i <= ms11; i++){
for (j = 1; j <= ms12; j++){
printf("\t %d ", matrix1[i][j]);
}
printf("\n");
}
printf("\n The 2nd Matrix is : \n\n"); // Print 2nd Matrix
for (i = 1; i <= ms21; i++){
for (j = 1; j <= ms22; j++){
printf("\t %d ", matrix2[i][j]);
}
printf("\n");
}

// . . . The two matrices have been multipled here . . .

for (i = 1; i <= ms11; i++){
for (j = 1; j <= ms22; j++){
matrix[i][j] = 0;
for (k = 1; k <= ms12; k++){
matrix[i][j] = matrix[i][j]+ matrix1[i][k] * matrix2[k][j];
}
}
}

// . . . Product of the given two Matrices is displayed in Matrix form . . .

printf("\n\n The Multiplied Resultant Matrix is \n\n");
for (i = 1; i <= ms11; i++){
for (j = 1; j <= ms22; j++){
printf("\t%d", matrix[i][j]);
}
printf("\n");
}

}

else

printf("\n\n The given Matrix sizes are not compatible to multiply");

getch();
return (0);
}

Sample Output:

Enter the size of 1st Matrix :  4    3

Enter the size of 2nd Matrix :   3    4

Enter the elements of 1st Matrix :

2       2       2       2       2       2       2       2       2       2       2       2

Enter the elements of 2nd Matrix:

2       2       2       2       2       2       2       2       2       2       2       2

The 1st Matrix is ::

2          2         2

2          2         2

2          2         2

2          2          2

The 2nd Matrix is :  :

2          2         2         2

2          2         2          2

2          2         2         2

The Multiplied Resultant Matrix is

12          12           12          12

12          12          12          12

12          12           12          12

12          12          12          12

Related Popular Posts:

*  Write a c program to check whether a given number is prime number or not?

*  Write a C program to find the prime numbers between 1 and 100

*  Write a C program to display the prime numbers between a range ?

*  Write a C program to find the prime numbers between a range using while loop?

*  Write a C program to find the sum of prime numbers between a range

About Er. T.Tharesh Kumar

I am a Professor and Head of Department of Computer Science in an Arts and Science College. I have designed this website to provide solved example programs of different categories for all the school and college going students to learn C language deeply, and to practice Interview based Multiple Choice (Objective Type) questions and answers.