To write this program, we need to understand the concept of invertible. A matrix (A)n ×n is said to be invertible if and only if there exists another matrix (B)n ×n such that AB=BA=ln. A matrix is invertible if and only if its determinant is not zero. A matrix is singular or not invertible if and only if its determinant is zero.
Take a matrix such as A and find out its determinant if its determinant is zero then A is invertible. All the square matrixes are not invertible. Example:

Consider a matrix A as shown below

Take its determinant

As determinant is not equal to zero, matrix A is invertible.

Check if a Matrix is Invertible using C++

Here is the source code of the program in C++ to Check if a Matrix is Invertible using. This program is successfully compiled, and the output is also given below.

#include<iostream>
using namespace std;

int
main () 
{
  
int matrix[3][3];
  
 
cout << "\nEnter 9 elements of matrix ";
  
for (int i = 0; i < 3; i++)
    
    {
      
for (int j = 0; j < 3; j++)
	
	{
	  
cin >> matrix[i][j];
    
} 
} 
 
cout << "\nMatrix is :";
  
for (int i = 0; i < 3; i++)
    
    {
      
for (int j = 0; j < 3; j++)
	
	{
	  
cout << matrix[i][j] << endl;
    
} 
} 
int det =
    matrix[0][0] * ((matrix[1][1] * matrix[2][2]) -
		    (matrix[2][1] * matrix[1][2])) -
    matrix[0][1] * (matrix[1][0] * matrix[2][2] -
		    matrix[2][0] * matrix[1][2]) +
    matrix[0][2] * (matrix[1][0] * matrix[2][1] -
		    matrix[2][0] * matrix[1][1]);
  
 
if (det == 0)
    
    {
      
cout << "\nMatrix is not invertible";
    
}
  
  else
    
    {
      
cout << "\nDeterminent is : " << det;
      
cout <<
	"\nAs Determinent is not equal to Zero, So matrix is invertible";
    
}
  
 
return 0;

}

Output

Program Explanation

C++ program starts execution from the main method. First of all, we declared a 2-D array of size 3 by 3 and input values form the user using nested for loop. Again, for loop is used to show the input values which was taken from the user.

A variable det is declared to store the calculated value of the determinant. After calculation of determinant, the value will be stored in det variable. If-else statement is used to check whether the matrix is invertible or not, if the value of the determinant is equal to zero, then the matrix is not invertible otherwise else part will execute, and message matrix is invertible will be printed on the screen.

Related Articles

Last modified: May 2, 2019