# C++ Program to Find if a Matrix is Invertible or Not

«
»

This is a C++ Program to Find if a Matrix is Invertible or Not.

Problem Description

The program takes a matrix and checks if it is invertible. An invertible matrix is a square matrix whose determinant exists.

Problem Solution

1. The program takes a square matrix.
2. Using a function, determinant of the matrix is calculated.
3. If determinant is equal to 0, it is not an invertible matrix.
4. Else it is an invertible matrix.
5. The result is printed.
6. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if a Matrix is Invertible or Not. The program output is shown below.

1. `#include<iostream>`
2. `#include<math.h> `
3. `using namespace std;`
4. `double det(int n, double mat)`
5. `{`
6. `    int subj = 0, subi = 0, i, j, c;`
7. `    double submat, d = 0;`
8. `    if (n == 2)`
9. `        return ((mat * mat) - (mat * mat));`
10. `    else`
11. `    {`
12. `        for (c = 0; c < n; c++)`
13. `        {`
14. `            for (i = 1; i < n; i++)`
15. `            {`
16. `                for (j = 0; j < n; j++)`
17. `                {`
18. `                    if (j == c)`
19. `                        continue;`
20. `                    submat[subi][subj] = mat[i][j];`
21. `                    subj++;`
22. `                }`
23. `                subi++;`
24. `             }`
25. `            d = d + (pow(-1, c) * mat[c] * det(n - 1, submat));`
26. `        }`
27. `    }`
28. `    return d;`
29. `}`
30. `int main(int argc, char **argv)`
31. `{`
32. `    int n, i, j;`
33. `    double mat;`
34. `	cout << "Enter order of the matrix : ";`
35. `    cin >> n;`
36. `    cout << "Enter the elements of the matrix : ";`
37. `    for (i = 0; i < n; i++)`
38. `        for (j = 0; j < n; j++)`
39. `            cin >> mat[j][i];`
40. `    if (det(n, mat) != 0)`
41. `        cout << "The given matrix is invertible.\n ";`
42. `    else`
43. `        cout << "The given matrix is not invertible.\n ";`
44. `    for (i = 0; i < n; i++)`
45. `    {`
46. `        for (j = 0; j < n; j++)	`
47. `            cout << mat[i][j] << " ";`
48. `        cout << "\n ";`
49. `    }`
50. `}`
Program Explanation

1. The user is asked to enter the order of the matrix, i.e the size. It is stored in the variable ‘n’.
2. The elements of the matrix are asked to enter and stored in ‘mat’.
3. Using a function ‘det’, the determinant of the matrix is calculated.
4. In the main function, the value of determinant is returned. If it is equal to 0, the matrix is not invertible.
5. Else the entered matrix is invertible. The matrix is printed.
6. The result is then printed.

Runtime Test Cases
```Case 1 :
Enter order of the matrix : 2
Enter the elements of the matrix : 2 -3 -1 2
The given matrix is invertible.
2 -1
-3 2

Case 2 :
Enter order of the matrix : 3
Enter the elements of the matrix : 1 2 3 4 5 6 7 8 9
The given matrix is not invertible.
1 4 7
2 5 8
3 6 9

Case 3 :
Enter order of the matrix : 3
Enter the elements of the matrix : 1 0 0 0 1 0 0 0 1
The given matrix is invertible.
1 0 0
0 1 0
0 0 1```

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples. 