C Program to Find Determinant of a Matrix

This C program computes the determinant of a matrix.

Here is the source code of the C program to find determinant 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.  
  3. int main(){
  4.  
  5.   int a[3][3], i, j;
  6.  
  7.   long determinant;
  8.   printf("Enter the 9 elements of matrix: ");
  9.   for(i = 0 ;i < 3;i++)
  10.       for(j = 0;j < 3;j++)
  11.            scanf("%d", &a[i][j]);
  12.  
  13.   printf("\nThe matrix is\n");
  14.   for(i = 0;i < 3; i++){
  15.       printf("\n");
  16.       for(j = 0;j < 3; j++)
  17.            printf("%d\t", a[i][j]);
  18.   }
  19.  
  20.   determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
  21.    * a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
  22.  
  23.   printf("\nDeterminant of 3X3 matrix: %ld", determinant);
  24.  
  25.    return 0;
  26. }

$ gcc determinant.c -o determinant
$ ./determinant
 
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	
Determinant of 3X3 matrix: -5

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.