C Program to find Determinant of Matrix (Any Order)

In this example, we are going to find Determinant of Square Matrix of any order. Finding determinant can be difficult if we follow normal approach that we learned in school mathematics.

In this program, we first convert given square matrix to upper triangular matrix using Gauss Elimination Technique then determinant is calculated simply by multiplying principle diagonal elements.

Input Matrix:
a    b    c
d    e    f
g    h    i

Is Changed To:
a    b    c
0    e'   f'
0    0    i''

Then Determinant Is:
a * e' * i''

Note: Here e' represents value of e is 
changed once and i'' represents value of 
i is changed twice during row operation.

Determinant of Matrix C Program


/*
Program: Finding Determinant of Matrix of Order N
*/


#include<stdio.h>
#include<math.h>
#include<stdlib.h>

#define   SIZE   10


int main()
{
	 float a[SIZE][SIZE], x[SIZE], ratio, det=1;
	 int i,j,k,n;

	 /* Inputs */
	 /* 1. Reading number of unknowns */
	 printf("Enter Order of Matrix: ");
	 scanf("%d", &n);

	 /* 2. Reading Matrix */
	 printf("\nEnter Coefficients of Matrix: \n");
	 for(i=0;i< n;i++)
	 {
		  for(j=0;j< n;j++)
		  {
			   printf("a[%d][%d]=",i,j);
			   scanf("%f", &a[i][j]);
		  }
	 }

    /* Here we are using Gauss Elimination
    Technique for transforming matrix to
    upper triangular matrix */
	/* Applying Gauss Elimination */
	 for(i=0;i< n;i++)
	 {
		  if(a[i][i] == 0.0)
		  {
			   printf("Mathematical Error!");
			   exit(0);
		  }
		  for(j=i+1;j< n;j++)
		  {
			   ratio = a[j][i]/a[i][i];

			   for(k=0;k< n;k++)
			   {
			  		a[j][k] = a[j][k] - ratio*a[i][k];
			   }
		  }
	 }

	 /* Displaying upper triangular matrix */
	 
	 /* Not required, just for the sake of understanding */
	 
	 /* By analyzing upper triangular matrix you 
	 will get what's going on :) */
	 printf("\nUpper Triangular Matrix: \n");
	 for(i=0;i< n;i++)
	 {
		  for(j=0;j< n;j++)
		  {
			   printf("%0.2f\t",a[i][j]);
		  }
		  printf("\n");
	 }

	 /* Finding determinant by multiplying
	 elements in principal diagonal elements */
	 for(i=0;i< n;i++)
     {
         det = det * a[i][i];
     }

	 printf("\n\nDeterminant of given matrix is: %0.3f", det);


	 return 0;
}

Output

Enter Order of Matrix: 3

Enter Coefficients of Matrix:
a[0][0]=1
a[0][1]=1
a[0][2]=1
a[1][0]=2
a[1][1]=-3
a[1][2]=4
a[2][0]=3
a[2][1]=4
a[2][2]=5

Upper Triangular Matrix:
1.00    1.00    1.00
0.00    -5.00   2.00
0.00    -0.00   2.40


Determinant of given matrix is: -12.000