
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Check Multiplicability of Two Matrices
Two matrices are said to be multiplicable if they can be multiplied. This is only possible if the number of columns of the first matrix is equal to the number of rows of the second matrix. For example.
Number of rows in Matrix 1 = 3 Number of columns in Matrix 1 = 2 Number of rows in Matrix 2 = 2 Number of columns in Matrix 2 = 5 Matrix 1 and Matrix 2 are multiplicable as the number of columns of Matrix 1 is equal to the number of rows of Matrix 2.
A program to check the multiplicity of two matrices is as follows.
Example
#include<iostream> using namespace std; int main() { int row1, column1, row2, column2; cout<<"Enter the dimensions of the first matrix:"<< endl; cin>>row1; cin>>column1; cout<<"Enter the dimensions of the second matrix: "<<endl; cin>>row2; cin>>column2; cout<<"First Matrix"<<endl; cout<<"Number of rows: "<<row1<<endl; cout<<"Number of columns: "<<column1<<endl; cout<<"Second Matrix"<<endl; cout<<"Number of rows: "<<row2<<endl; cout<<"Number of columns: "<<column2<<endl; if(column1 == row2) cout<<"Matrices are multiplicable"; else cout<<"Matrices are not multiplicable"; return 0; }
output
Enter the dimensions of the first matrix: 2 3 Enter the dimensions of the second matrix: 3 3 First Matrix Number of rows: 2 Number of columns: 3 Second Matrix Number of rows: 3 Number of columns: 3 Matrices are multiplicable
In the above program, first the dimensions of the two matrices are entered by the user. This is shown as follows.
cout<<"Enter the dimensions of the first matrix:"<< endl; cin>>row1; cin>>column1; cout<<"Enter the dimensions of the second matrix: "<<endl; cin>>row2; cin>>column2;
After that, the number of rows and columns of the matrices are printed. This is shown below.
cout<<"First Matrix"<<endl; cout<<"Number of rows: "<<row1<<endl; cout<<"Number of columns: "<<column1<<endl; cout<<"Second Matrix"<<endl; cout<<"Number of rows: "<<row2<<endl; cout<<"Number of columns: "<<column2<<endl;
If the number of columns in matrix1 is equal to the number of rows in matrix2, then it is printed that the matrices are multiplicable. Otherwise, it is printed that the matrices are not multiplicable. This is demonstrated by the following code snippet.
if(column1 == row2) cout<<"Matrices are multiplicable"; else cout<<"Matrices are not multiplicable";
- Related Articles
- C# program to check if two matrices are identical
- Java program to check if two given matrices are identical
- Python Program to check if two given matrices are identical
- Program to check if two given matrices are identical in C++
- Golang Program to Check Whether Two Matrices are Equal or Not
- Swift Program to Check Whether Two Matrices Are Equal or Not
- C# program to multiply two matrices
- Java program to add two matrices.
- Java program to subtract two matrices.
- Java program to multiply two matrices.
- C# program to add two matrices
- Python program to multiply two matrices
- Golang Program To Add Two Matrices
- Program to multiply two matrices in C++
- Program for subtracting two matrices.

Advertisements