# Multiply Two Matrix

“** Multiply two matrix**” is a standard operation which can be applied over two or more matrices. Here, we are given two matrices of dimension

**and**

*r1*c1***respectively. Our task is to multiply the following matrices and store the result in resultant matrix.**

*r2*c2**CONDITION : The two matrices can only be added if the number of rows of first matrix is equal to number of columns of second matrix.*

**Example:**

R1 = 2, C1 =3

R2 = 3 C2 = 2

1 | 2 | 3 |

1 | 2 | 3 |

4 | 5 |

4 | 5 |

4 | 5 |

24 | 30 |

24 | 30 |

**C++ Program to add two matrices is as follows:**

#include<bits/stdc++.h> using namespace std; int main() { int r1,c1,r2,c2; // Scan Dimensions of the matrix cout<<"Enter the dimensions of first matrix:\n"; cin>>r1>>c1; cout<<"Enter the dimensions of second matrix:\n"; cin>>r2>>c2; int first[r1][c1]; int second[r2][c2]; int resultant[r1][c2]; // Scan Matrix1 Elements cout<<"Enter the elements of first matrix:\n"; for(int i = 0 ; i < r1 ; i++) for(int j = 0 ; j < c1 ; j++) cin>>first[i][j]; // Scan Matrix2 Elements cout<<"Enter the elements of second matrix:\n"; for(int i = 0 ; i < r2 ; i++) for(int j = 0 ; j < c2 ; j++) cin>>second[i][j]; // Initialize the resultant matrix with 0 for(int i = 0 ; i < r1 ; i++) for(int j = 0 ; j < c2 ; j++) resultant[i][j] = 0; // Multiply matrix for (int i = 0; i < r1; ++i) { for (int j = 0; j < c2; ++j) { for (int k = 0; k < c1; ++k) { resultant[i][j] += first[i][k] * second[k][j]; } } } //Print Resultant matrix cout<<"The Resultant Matrix is:\n"; for(int i = 0 ; i < r1 ; i++) { for(int j = 0 ; j < c2 ; j++) { cout<<resultant[i][j]<<" "; } cout<<endl; } }

Enter the dimensions of first matrix: 2 3 Enter the elements of second matrix:3 2 Enter the elements of first matrix: 1 2 3 1 2 3 Enter the elements of second matrix: 4 5 4 5 4 5 The Resultant Matrix is: 24 30 24 30OUTPUT:

**Related Posts:**

**Program to check Identity Matrix.****Program to check Idempotent Matrix.****Program to check Involuntary Matrix.****Program to print matrix in zig-zag order.****Program to print matrix in spiral order.****Program to sort a matrix.****Program to Add two Matrix.****Program to Transpose a Matrix.****Program to find smallest and largest word of the String.****Program to find Most Frequent Character of the String.****Program to Remove all the blank spaces from the String.****Program to check if String is isogram or not.****Program to Reverse Each word of the String.****Program to Print All the Substring of the Given String.****Program to find longest palindromic Substring.****Program to check Anagram Strings.****Program to check whether two Strings are Rotation of each other or not.****Program to check Palindromic Anagram.****Program to print all the Palindromic Substring of the String.****Program to check Panagram String.**