C program to find inverse of a matrix






How to find inverse of a matrix in c
C code to find inverse of a matrix
Inverse of a 3x3 matrix in c

#include<stdio.h>

int main(){

  int a[3][3],i,j;
  float determinant=0;

  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }

  for(i=0;i<3;i++)
      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

   printf("\nInverse of matrix is: \n\n");
   for(i=0;i<3;i++){
      for(j=0;j<3;j++)
           printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
       printf("\n");
   }

   return 0;
}

Enter the 9 elements of matrix: 3
5
2
1
5
8
3
9
2

The matrix is

3       5       2
1       5       8
3       9       2
Inverse of matrix is:

0.70    -0.25   0.07
-0.09   -0.00   0.14
-0.34   0.25    -0.11






Alogrithm:
**







16 comments:

Anonymous said...

i am new to programming.can anyone pls explain this to me.offer ur help as soon as possible

Ronald Michaels said...

Program above prints transpose of inverse.
see below revised code

printf("\nInverse of matrix is: \n\n");
// for(i=0;i<3;i++){
// for(j=0;j<3;j++)
for(j=0;j<3;j++){
for(i=0;i<3;i++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) -
(a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
}
printf("\n");
}

Khal said...

what about 2by2 ??

damd said...

instead of 3 use 2 . yu need to replace dem by 2!
and also dont forget to initialise st[2]

Anonymous said...

Is program correctly work or not ?

humza tufail said...

yes corectly work thanks ritesh:)

Anonymous said...

Good, but for a matrix it is also important that its determinant must be non-zero for existence of inverse.
Thus it will be good to include one more condition so it will immediately tell whether the matrix entered is valid or not.
Rest of the things are okay :)

Anonymous said...

i want 2*2 programme now! pleaseeeeee.

Anonymous said...

It does some approximations making it useless. 0.04 gets rounded to zero. 0.06 gets rounded to 0.1

Anonymous said...

if u want higher accuracy in the digits, then replace %.2f with %.10f and you will have 10 digits of accuracy instead of 2

Venki Chelladurai said...

it works i tried it

Anonymous said...

HOW ABOUT 6X6 MATRIX?

Kamran Khan said...

why you use %3 finding determinant

j.prasanna kumar said...

write a c program to display the elements in the matrix from any starting point it should travel upside down and also want to end at starting point.
pls help me @ richardprasanna@outlook.com

Ranjeet kumar said...

hello ever one

Unknown said...

I can't understand why u used %3 in the code written above is it possible to write the code without %3