Here you will get C and C++ program to find inverse of a matrix.

We can obtain matrix inverse by following method.

- First calculate deteminant of matrix.
- Then calculate adjoint of given matrix. Adjoint can be obtained by taking transpose of cofactor matrix of given square matrix.
- Finally multiply 1/deteminant by adjoint to get inverse.

The formula to find inverse of matrix is given below.

You can watch below video to learn how inverse is calculated.

In below program I have calculated the inverse of 3×3 matrix.

## C Program to Find Inverse of a Matrix

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include<stdio.h> int main(){ int mat[3][3], i, j; float determinant = 0; printf("Enter elements of matrix row wise:\n"); for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) scanf("%d", &mat[i][j]); printf("\nGiven matrix is:"); for(i = 0; i < 3; i++){ printf("\n"); for(j = 0; j < 3; j++) printf("%d\t", mat[i][j]); } //finding determinant for(i = 0; i < 3; i++) determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3])); printf("\n\ndeterminant: %f\n", determinant); printf("\nInverse of matrix is: \n"); for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++) printf("%.2f\t",((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant); printf("\n"); } return 0; } |

## C++ Program to Find Inverse of a Matrix

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include<iostream> using namespace std; int main(){ int mat[3][3], i, j; float determinant = 0; cout<<"Enter elements of matrix row wise:\n"; for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) cin>>mat[i][j]; printf("\nGiven matrix is:"); for(i = 0; i < 3; i++){ cout<<"\n"; for(j = 0; j < 3; j++) cout<<mat[i][j]<<"\t"; } //finding determinant for(i = 0; i < 3; i++) determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3])); cout<<"\n\ndeterminant: "<<determinant; cout<<"\n\nInverse of matrix is: \n"; for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++) cout<<((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant<<"\t"; cout<<"\n"; } return 0; } |

**Output**

thank you very much for your example, now i can go to bed easily =D . Really help my task to do inverse matrices from read files

why you use %3 finding determinant

Matrix size 3

Write a program in C++ to find the minor of every elements of a square matrix of any order.

The order of the square matrix should be entered by the user.

what is the function of the mode in the code

Thank u so much dude… u’ve helped us..

I did not understand this can you explain me clear ‘

cout<<((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) – (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant<<"\t";

what is J+1, i+1 and so on?

Thank you very much to upload this program

Bro i want the explanation about the full program if u have any related videos plz send to my mail

What would be the changes to calculate 4*4 matrix or further ?

i don’t understand (j+1)%3 and (i+1)%3