# C Program to find Inverse of Matrix using while loop

## C Program to find Inverse of Matrix using while loop

This page has a C Program to find Inverse of Matrix using while loop. It is clear that, C program has been written to find Inverse of Matrix using while loop for any size of square matrix. The Inverse of matrix is calculated by using few steps. To find  Inverse of matrix, we need to find the determinant of matrix first. If the determinant of nxn matrix is non zero, then we can find Inverse of matrix. If the determinant of nxn matrix is zero, then we can not 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 Inverse of Matrix using while loop. Kindly check out the program to display Inverse of Matrix using while loop  for any n x n matrix. The questions to find the Inverse of Matrix using while loop can be asked as,

1). C program to find the Inverse of Matrix using while loop
2). C program to find Inverse of  any size of square Matrix using while loop
3). C Program to Find Inverse Of n x n 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  square Matrix using while loop

7). C Program to find the Inverse of a Square Matrix
8). Find Inverse of nxn matrix using C
9). Find Inverse of  2x2, 3x3, 4x4, 5x5, . . . square Matrix using while loop
10). C Program to Determine whether a matrix has an inverse

### Find Inverse of  any size of square Matrix using while loop Program

// C Program helps to find the Inverse of Matrix of 4x4 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 [][20], float [][20], int); //for transpose of matrix
void cofac(float [][20], int); //for cofactors of matrix
float determin(float [][20], int); //for determinant of matrix
int main(){
float matrix[20][20], deter;
int a=0, b=0, row, col;
clrscr();
printf("\n Enter the size of matrix : ");
scanf("%d %d", &row, &col);
if(row==col){
printf("Enter the elements of %d X %d matrix : \n", row, col);
while(a<row){
for(b=0;b<col;b++){
scanf("%f",&matrix[a][b]);
}
++a;
}
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 The given Matrix is not compatible to find Inverse \n");
getch();
return(0);
}

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

float determin(float matrix[20][20], int k){
float deter=0.0, z=1.0, mt[20][20];
int a=0, b, c, x, y;
if(k==1)
{
return(matrix[0][0]);
}
else
{
deter=0;
while(a<k){
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;
++a;
}
}
return(deter);
}

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

void cofac(float comatr[20][20], int f){
float matr[20][20], cofact[20][20];
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[20][20], float m1[20][20], int r){
float inv_matrix[20][20], tran[20][20], d;
int a,b,a1=0,a2=0,a3=0;
while(a3<r){
for(b=0;b<r;++b){
tran[a3][b]=m1[b][a3];
}
a3++;
}
d=determin(matr, r);
while(a2<r){
for(b=0;b<r;++b){
inv_matrix[a2][b]=tran[a2][b] / d;
}
a2++;
}
printf("\n\n\n The Inverse of matrix is . . . \n\n");
while(a1<r){
for(b=0;b<r;++b)
printf("\t%f", inv_matrix[a1][b]);
printf("\n\n");
++a1;
}
}

Sample Output:

Enter the size of the Matrix : 4     4

Enter elements of  4 X 4  matrix :

1     -2     4     1     3     1     0     -2     1     0     2     4     -3     5     -2     1

Determinant of the Matrix is :   -224

The Inverse of matrix of  4 x 4 matrix is . . .

-0.133           0.223          0.169         -0.098

0.098            0.169          0.008         0.205

0.37             0.049          -0.102         0.138

-0.151           -0.08            0.258        -0.044

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