C Program to check if matrix is singular or not

Given a matrix as mat[row][column], our task is to check whether the given matrix is singular or not through a function and display the result.

A singular matrix is a matrix whose determinant is zero. If the determinant is not zero then the matrix is non-singular.

So to find whether the matrix is singular or non-singular we need to calculate determinant first. For a 3x3 matrix, the determinant can be calculated as −

$$M1[3][3]\:=\:\begin{bmatrix}a & b & c \d & e & f \g & h & i \end{bmatrix}$$

|m1| = a(e*i - f*h) - b(d*i - f*g) + c(d*h - e*g)

Syntax

int check_singular(int matrix[N][N], int n);
int cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n);

Example Input and Output

Input-: mat[3][3]= { {4, 10, 1},
                     {0, 2, 3},
                     {1, 4, -3} }
Output-: matrix is non-singular

Input-: mat[3][3]= { {0, 0, 0},
                     {10, 20, 30},
                     {1, 4, -3} }
Output-: matrix is singular
Since the entire first row is 0 the determinant will be zero only

Example: Complete C Program

This program calculates the determinant using cofactor expansion and checks if the matrix is singular −

#include <stdio.h>
#define N 3

// Function to find the cofactors
void cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n) {
    int i = 0, j = 0;
    int row, col;
    
    // Looping for each element of the matrix
    for (row = 0; row < n; row++) {
        for (col = 0; col < n; col++) {
            // Copying into temporary matrix only
            // those element which are not in given
            // row and column
            if (row != p && col != q) {
                matrix2[i][j++] = matrix[row][col];
                
                // Row is filled, so increase row
                // index and reset col index
                if (j == n - 1) {
                    j = 0;
                    i++;
                }
            }
        }
    }
}

/* Recursive function to check if matrix[][] is singular or not. */
int check_singular(int matrix[N][N], int n) {
    int D = 0; // Initialize result
    
    // Base case : if matrix contains single element
    if (n == 1)
        return matrix[0][0];
    
    int matrix2[N][N]; // To store cofactors
    int sign = 1; // To store sign multiplier
    
    // Iterate for each element of first row
    for (int f = 0; f < n; f++) {
        // Getting Cofactor of matrix[0][f]
        cofactor(matrix, matrix2, 0, f, n);
        D += sign * matrix[0][f] * check_singular(matrix2, n - 1);
        // terms are to be added with alternate sign
        sign = -sign;
    }
    return D;
}

// Driver program to test above functions
int main() {
    int matrix[N][N] = { {4, 10, 1},
                        {0, 2, 3},
                        {1, 4, -3} };
    
    printf("Given Matrix:
"); for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { printf("%d ", matrix[i][j]); } printf("
"); } int det = check_singular(matrix, N); printf("Determinant: %d
", det); if (det == 0) printf("Matrix is Singular
"); else printf("Matrix is non-Singular
"); return 0; }
Given Matrix:
4 10 1 
0 2 3 
1 4 -3 
Determinant: -50
Matrix is non-Singular

How It Works

  • The cofactor() function extracts a smaller matrix by removing a specific row and column.
  • The check_singular() function recursively calculates the determinant using cofactor expansion.
  • If the determinant is zero, the matrix is singular; otherwise, it's non-singular.
  • The algorithm uses the first row expansion method for determinant calculation.

Key Points

  • Time complexity is O(n!) due to recursive nature of determinant calculation.
  • A matrix is singular if and only if its determinant equals zero.
  • Singular matrices are non-invertible and have linearly dependent rows or columns.

Conclusion

This program successfully determines if a matrix is singular by calculating its determinant using cofactor expansion. The recursive approach handles matrices of any size defined by the constant N.

Updated on: 2026-03-15T12:16:48+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements