C Program to Check if a Matrix is Invertible

This C program checks if a matrix is invertible. The key idea is to first find out the determinant of the given matrix. If it is a non-zero quantity, then the given matrix is invertible, otherwise there is no inverse of that matrix.

Here is the source code of the C program to check for inverse of a matrix. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2.  int main(){
  3.  
  4.   int a[3][3], i, j;
  5.  
  6.   long determinant;
  7.   printf("Enter the 9 elements of matrix: ");
  8.   for(i = 0 ;i < 3;i++)
  9.       for(j = 0;j < 3;j++)
  10.            scanf("%d", &a[i][j]);
  11.  
  12.   printf("\nThe matrix is\n");
  13.   for(i = 0;i < 3; i++){
  14.       printf("\n");
  15.       for(j = 0;j < 3; j++)
  16.            printf("%d\t", a[i][j]);
  17.   }
  18.   determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
  19.    * a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
  20.    if ( determinant == 0)
  21.        printf("\nMatrix is NOT invertible");
  22.    else
  23.        printf("\nThe given matrix has an inverse!!!");
  24.    return 0;
  25. }

$ gcc matrix_invertible.c -o matrix_invertible
$ ./matrix_invertible
Enter the 9 elements of matrix: 1 2 3 4 5 1 2 3 4
The matrix is
 
1	2	3	
4	5	1	
2	3	4	
 
The given matrix has an inverse!!!

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.