Java Program to Find the Determinant of a Matrix

Last Updated : 21 Aug, 2026

The determinant is a single numerical value calculated from a square matrix. It is useful in solving systems of linear equations, finding matrix properties, and determining whether a matrix is singular or non-singular.

  • The determinant is calculated only for a square matrix.
  • It can be found using recursive cofactor expansion or non-recursive row operations.

Example:

Input: 4 3
2 3

Output: 6

Program to find Determinant of a Matrix - GeeksforGeeks

Methods to Find the Determinant

There are two common approaches:

1. Using Recursion

Approach

  • Check the size of the matrix.
  • If the matrix is 1 × 1, return its only element.
  • Create a cofactor matrix by removing one row and one column.
  • Recursively calculate the determinant of the cofactor.
  • Multiply it by the corresponding element and sign.
  • Add all the results.
Java
public class GFG {

    static int determinant(int[][] mat, int n) {

        // Base case
        if (n == 1) {
            return mat[0][0];
        }

        // Base case for 2 x 2 matrix
        if (n == 2) {
            return mat[0][0] * mat[1][1]
                 - mat[0][1] * mat[1][0];
        }

        int det = 0;

        for (int col = 0; col < n; col++) {

            int[][] subMatrix = new int[n - 1][n - 1];

            // Create cofactor matrix
            for (int i = 1; i < n; i++) {
                int subCol = 0;

                for (int j = 0; j < n; j++) {

                    if (j == col) {
                        continue;
                    }

                    subMatrix[i - 1][subCol++] = mat[i][j];
                }
            }

            int sign = (col % 2 == 0) ? 1 : -1;

            det += sign * mat[0][col]
                    * determinant(subMatrix, n - 1);
        }

        return det;
    }

    public static void main(String[] args) {

        int[][] mat = {
            {4, 3},
            {2, 3}
        };

        System.out.println(
            "Determinant: " +
            determinant(mat, mat.length)
        );
    }
}

Output
Determinant: 6

Explanation:

  • The program calculates the determinant by expanding the matrix along the first row.
  • It finds the cofactor of each element by removing its row and column.
  • It recursively calculates the determinant of each smaller matrix.
  • The results are multiplied by the corresponding elements and added with alternating signs.
  • For a 2 × 2 matrix { {4, 3}, {2, 3} }, the determinant is 6.

2. Using Gaussian Elimination

A more efficient approach is to transform the matrix into an upper triangular matrix.

Approach

  • Find a non-zero pivot for each column.
  • Swap rows if the pivot is zero.
  • Change the sign of the determinant when two rows are swapped.
  • Use the pivot to eliminate elements below it.
  • Multiply all diagonal elements to get the determinant.
Java
public class GFG {

    static double determinant(double[][] mat) {

        int n = mat.length;
        double det = 1;

        for (int i = 0; i < n; i++) {

            // Find pivot
            int pivot = i;

            for (int j = i + 1; j < n; j++) {
                if (Math.abs(mat[j][i]) >
                    Math.abs(mat[pivot][i])) {
                    pivot = j;
                }
            }

            // If pivot is zero, determinant is zero
            if (Math.abs(mat[pivot][i]) < 1e-9) {
                return 0;
            }

            // Swap rows if required
            if (pivot != i) {
                double[] temp = mat[i];
                mat[i] = mat[pivot];
                mat[pivot] = temp;

                det = -det;
            }

            // Eliminate elements below pivot
            for (int j = i + 1; j < n; j++) {

                double factor = mat[j][i] / mat[i][i];

                for (int k = i; k < n; k++) {
                    mat[j][k] -= factor * mat[i][k];
                }
            }
        }

        // Product of diagonal elements
        for (int i = 0; i < n; i++) {
            det *= mat[i][i];
        }

        return det;
    }

    public static void main(String[] args) {

        double[][] mat = {
            {1, 0, 2, -1},
            {3, 0, 0, 5},
            {2, 1, 4, -3},
            {1, 0, 5, 0}
        };

        System.out.println(
            "Determinant: " + determinant(mat)
        );
    }
}
Try It Yourself
redirect icon

Output
Determinant: 30.0

Explanation:

  • The program uses row operations to convert the matrix into an upper triangular matrix.
  • If a diagonal element is 0, it searches for a suitable row and swaps the rows.
  • Row swapping changes the sign of the determinant.
  • Elements below the main diagonal are eliminated using arithmetic operations.
  • Finally, the diagonal elements are multiplied to obtain the determinant.
  • For the given 4 × 4 matrix, the determinant is 30.
Comment