VOOZH about

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

⇱ Java Program to Find the Determinant of a Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Program to Find the Determinant of a Matrix

Last Updated : 1 May, 2025

The determinant of a matrix is a special calculated value that can only be calculated if the matrix has same number of rows and columns (square matrix). It is helpful in determining the system of linear equations, image processing, and determining whether the matrix is singular or non-singular.

In this article, we are going to learn the step-by-step procedure to calculate the determinant of a matrix and Java implementations using both recursive and non-recursive approaches.

Procedure to Calculate

  • First, we need to calculate the cofactor of all the elements of the matrix in the first row or first column.
  • Then, multiply each element of the first row or first column by their respective cofactor.
  • At last, we need to add them up with alternate signs.

Examples:

Determinant of 2*2 matrix:

[4, 3]
[2, 3]

= (4*3)-(3*2)
= 12-6
= 6


Determinant of 3*3 matrix:

[1, 3, -2]
[-1, 2, 1]
[1, 0, -2]

= 1(-4-0)-3(2-1)+(-2)(0-2)
= -4-3+4
= -3

Note:

  • The determinant of 1*1 matrix is the element itself.
  • The Cofactor of any element of the stated matrix can be calculated by eliminating the row and the column of that element from the matrix stated.

Let's see an example to get a clear concept of the above topic.

Determinant of a Matrix in Java

Example 1: Finding the determinant of a matrix using recursion.


Output
Determinant of the matrix is: 6

Time complexity: O(n3


Example 2:Non-recursive Implementation of finding determinant of a matrix.


Output
Determinant of the matrix is: 30

Time complexity: O(n3

Comment
Article Tags: