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:
**







12 comments:

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

    ReplyDelete
  2. 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");
    }

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

    ReplyDelete
  4. Is program correctly work or not ?

    ReplyDelete
  5. yes corectly work thanks ritesh:)

    ReplyDelete
  6. 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 :)

    ReplyDelete
  7. i want 2*2 programme now! pleaseeeeee.

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

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

    ReplyDelete
  10. HOW ABOUT 6X6 MATRIX?

    ReplyDelete