using System; class MatrixDeterminant { // Function to get the determinant of a matrix static int getDet(int[,] mat) { int n = mat.GetLength(0); int num1, num2, det = 1, index, total = 1; // Temporary array for storing row int[] temp = new int[n + 1]; // Loop for traversing the diagonal elements for (int i = 0; i < n; i++) { index = i; // Finding the index which has a non-zero value while (index < n && mat[index, i] == 0) { index++; } // If there is no non-zero element if (index == n) { // The determinant of the matrix is zero continue; } if (index != i) { // Loop for swapping the diagonal element // row and index row for (int j = 0; j < n; j++) { int tempSwap = mat[index, j]; mat[index, j] = mat[i, j]; mat[i, j] = tempSwap; } // Determinant sign changes when we shift rows det *= (int)Math.Pow(-1, index - i); } // Storing the values of diagonal row elements for (int j = 0; j < n; j++) { temp[j] = mat[i, j]; } // Traversing every row below the diagonal element for (int j = i + 1; j < n; j++) { num1 = temp[i]; // Value of diagonal element num2 = mat[j, i]; // Value of next row element // Traversing every column of row and multiplying // to every row for (int k = 0; k < n; k++) { // Making the diagonal element and next row // element equal mat[j, k] = (num1 * mat[j, k]) - (num2 * temp[k]); } total *= num1; } } // Multiplying the diagonal elements to get determinant for (int i = 0; i < n; i++) { det *= mat[i, i]; } return (det / total); // Det(kA)/k = Det(A); } // Driver code static void Main() { int[,] mat = { { 1, 0, 2, -1 }, { 3, 0, 0, 5 }, { 2, 1, 4, -3 }, { 1, 0, 5, 0 } }; Console.WriteLine(getDet(mat)); } }