VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-find-determinant-of-a-matrix/

⇱ C Program to Find Determinant of a Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Find Determinant of a Matrix

Last Updated : 23 Jul, 2025

What is the Determinant of a Matrix? 
The determinant of a Matrix is a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used at many places in calculus and other matrices related to algebra, it actually represents the matrix in terms of a real number which can be used in solving a system of a linear equation and finding the inverse of a matrix.

How to calculate? 
The value of the determinant of a matrix can be calculated by the following procedure – 
For each element of the first row or first column get the cofactor of those elements and then multiply the element with the determinant of the corresponding cofactor, and finally add them with alternate signs. As a base case, the value of the determinant of a 1*1 matrix is the single value itself. 

Cofactor of an element is a matrix that we can get by removing the row and column of that element from that matrix.

Determinant of 2 x 2 Matrix:

👁 22

Determinant of 3 x 3 Matrix: 
 

👁 Image


Output
Determinant of the matrix is : 30

Time Complexity: O(n4)
Space Complexity: O(n2), Auxiliary space used for storing cofactors.

Efficient Approach:

Determinant of a Matrix using Determinant properties:

  • In this method, we are using the properties of Determinant. 
  • Converting the given matrix into an upper triangular matrix using determinant properties 
  • The determinant of the upper triangular matrix is the product of all diagonal elements. 
  • Iterating every diagonal element and making all the elements down the diagonal as zero using determinant properties 
  • If the diagonal element is zero then search for the next non-zero element in the same column.

There exist two cases:

  • Case 1: If there is no non-zero element. In this case, the determinant of a matrix is zero 
  • Case 2: If there exists a non-zero element there exist two cases:
    • Case A: If the index is with a respective diagonal row element. Using the determinant properties make all the column elements down                       to it zero
    • Case B: Swap the row with respect to the diagonal element column and continue the Case A operation.

Below is the implementation of the above approach:


Output
Determinant of the matrix is : 30

Time Complexity: O(N*N*N), where N is the size of the matrix

Space Complexity: O(N) as temp array has been created to store row.

For more details, refer to the article - Determinant of a Matrix

Comment
Article Tags: