# C Program to findInverse of matrix

## C Program to find the Inverse of matrix

This page has a C Program to find the Inverse of matrix for any size of matrices. It is clear that, C program has been written by me to find the Inverse of matrix for any size of square matrix. The Inverse of matrix is calculated by using few steps. To find  Inverse of matrix, we should find the determinant of matrix first. If the determinant of matrix is non zero, we can find Inverse of matrix. If the determinant of matrix is zero, we can not find the Inverse of matrix. If the determinant of matrix is non zero, then we can find the Inverse of matrix. To find the Inverse of matrix we need to find the Cofactors for each elements of the matrix. The below given C program will find the Inverse of any square matrix. Kindly check out the program to display the Inverse of any sizes of matrices. The questions for the Inverse of matrix can be asked as,

1). C program to find Inverse of n x n matrix
2). C program to find inverse of a matrix
3). C Program to Find Inverse Of 3 x 3 Matrix
4). C Program to calculate inverse of a matrix
5). C Program to find the Inverse of a Matrix
6). C program to find inverse of matrix
7). C Program to find the Inverse of a Square Matrix
8). Inverse of nxn matrix using C
9). Inverse of a Matrix using Minors, Cofactors and Adjugate
10). C Program to Determine whether a matrix has an inverse

### Inverse of matrix Program

// C Program helps to find Inverse of Matrix for any size of Square matrix !!

// I (Er. T. Tharesh Kumar) created the Inverse of matrix program to you all !

#include<stdio.h>
#include<math.h>
#include<conio.h>
void trans(float [][40], float [][40], int); //for transpose of matrix
void cofac(float [][40], int); //for cofactors of matrix
float determin(float [][40], int); //for determinant of matrix
int main(){
float matrix[40][40], deter;
int a, b, row, col;
clrscr();
printf("Enter size of the Matrix : ");
scanf("%d %d", &row, &col);
if(row==col){
printf("Enter the elements of %d X %d matrix : \n", row, col);
for(a=0;a<row;++a){
for(b=0;b<col;++b){
scanf("%f",&matrix[a][b]);
}
}
deter = determin(matrix, row);
printf("\n The Determinant of the Matrix is : %f", deter);
if(deter == 0)
printf("\n Inverse of Matrix is not possible for the matrices having 0 Determinant value !!\n");
else
cofac(matrix, row);
}
else
printf("\n Inverse of Matrix is possible for Square matrix !! Kindly give same number of rows and columns \n");
getch();
return(0);
}

// This function is to find the determinant value of matrix . . .

float determin(float matrix[40][40], int k){
float deter=0.0, z=1.0, mt[40][40];
int a, b, c, x, y;
if(k==1)
{
return(matrix[0][0]);
}
else
{
deter=0;
for(a=0;a<k;++a){
x=0;
y=0;
for(b=0;b<k;++b){
for(c=0;c<k;++c){
mt[b][c]=0;
if((b != 0) && (c != a))
{
mt[x][y]=matrix[b][c];
if(y<(k-2))
y++;
else
{
y=0;
x++;
}
}
}
}
deter=deter + z * (matrix[0][a] * determin(mt,k-1));
z=-1 * z;
}
}
return(deter);
}

// This function is to find cofactors of matrix . . .

void cofac(float comatr[40][40], int f){
float matr[40][40], cofact[40][40];
int a, b, c, d, x, y;
for(c=0; c<f; ++c){
for(d=0; d<f; ++d){
x=0;
y=0;
for(a=0;a<f; ++a){
for(b=0; b<f; ++b){
if(a != c && b != d)
{
matr[x][y]=comatr[a][b];
if(y<(f-2))
y++;
else
{
y=0;
x++;
}
}
}
}
cofact[c][d] = pow(-1,c + d) * determin(matr,f-1);
}
}
trans(comatr, cofact, f);
}

// This function is to transpose of matrix . . .

void trans(float matr[40][40], float m1[40][40], int r){
float inv_matrix[40][40], tran[40][40], d;
int a,b;
for(a=0;a<r;++a){
for(b=0;b<r;++b){
tran[a][b]=m1[b][a];
}
}
d=determin(matr, r);
for(a=0;a<r;++a){
for(b=0;b<r;++b){
inv_matrix[a][b]=tran[a][b] / d;
}
}
printf("\n\n\n The Inverse of matrix is . . . \n\n");
for(a=0;a<r;++a){
for(b=0;b<r;++b)
printf("\t%f", inv_matrix[a][b]);
printf("\n\n");
}
}

Sample Output:

Enter the size of the Matrix : 2     2

Enter elements of  2 X 2  matrix :

1     2     3      4

Determinant of the Matrix is :   -2

The Inverse of matrix is . . .

-2              1

1.5          -0.5

--------------------------------------------------------------------------------------------
Enter the size of the Matrix : 3     3

Enter elements of  3 X 3  matrix :

2     1     3      6      1      2      7      2      4

Determinant of the Matrix is :   5

The Inverse of matrix is . . .

0           0.4          -0.2

-2          -2.6         2.8

1            0.6         -0.8

--------------------------------------------------------------------------------------------

Enter the size of the Matrix : 4     4

Enter elements of  4 X 4  matrix :

2     1     8     7      9      4     2     3     6      5      0      4      2      3      6       8

Determinant of the Matrix is :   -2.0

The Inverse of matrix is . . .

31           -19           30          -35

-94            58           -91          106

-58            36          -56.5        65.5

71           -44            69           -80

Related Popular Posts:

*  Write a c program to check whether a given number is prime number or not?

*  Write a C program to find the prime numbers between 1 and 100

*  Write a C program to display the prime numbers between a range ?

*  Write a C program to find the prime numbers between a range using while loop?

*  Write a C program to find the sum of prime numbers between a range

About Er. T.Tharesh Kumar

I am a Professor and Head of Department of Computer Science in an Arts and Science College. I have designed this website to provide solved example programs of different categories for all the school and college going students to learn C language deeply, and to practice Interview based Multiple Choice (Objective Type) questions and answers.