using System; class MatrixOperations { // Function to get cofactor of mat[p][q] in cof[][]. n is // current dimension of mat[][] static void GetCof(int[,] mat, int[,] cof, int p, int q, int n) { int i = 0, j = 0; for (int row = 0; row < n; row++) { for (int col = 0; col < n; col++) { if (row != p && col != q) { cof[i, j++] = mat[row, col]; if (j == n - 1) { j = 0; i++; } } } } } // Recursive function for finding determinant of matrix mat of dimension n static int GetDet(int[,] mat, int n) { if (n == 1) return mat[0, 0]; int det = 0; // To store cofactors int[,] cof = new int[n, n]; int sign = 1; for (int f = 0; f < n; f++) { GetCof(mat, cof, 0, f, n); det += sign * mat[0, f] * GetDet(cof, n - 1); sign = -sign; } return det; } // Function to get adjoint of mat in adj static void Adjoint(int[,] mat, double[,] adj) { int n = mat.GetLength(0); if (n == 1) { adj[0, 0] = 1; return; } int sign = 1; int[,] cof = new int[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { GetCof(mat, cof, i, j, n); sign = ((i + j) % 2 == 0) ? 1 : -1; adj[j, i] = sign * GetDet(cof, n - 1); } } } // Function to calculate and store inverse, returns false if matrix is singular static bool Inverse(int[,] mat, double[,] inv) { int n = mat.GetLength(0); int det = GetDet(mat, n); if (det == 0) { Console.WriteLine("Singular matrix, can't find its inverse"); return false; } double[,] adj = new double[n, n]; Adjoint(mat, adj); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) inv[i, j] = adj[i, j] / det; return true; } static void Main() { int[,] mat = new int[,] { { 5, -2, 2, 7 }, { 1, 0, 0, 3 }, { -3, 1, 5, 0 }, { 3, -1, -9, 4 } }; int n = mat.GetLength(0); double[,] adj = new double[n, n]; // To store adjoint double[,] inv = new double[n, n]; // To store inverse // Print the input matrix Console.WriteLine("Input matrix is:"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(mat[i, j] + " "); } Console.WriteLine(); } // Print the adjoint matrix Console.WriteLine("\nThe Adjoint is:"); Adjoint(mat, adj); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(adj[i, j] + " "); } Console.WriteLine(); } // Print the inverse matrix if it exists Console.WriteLine("\nThe Inverse is:"); if (Inverse(mat, inv)) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(inv[i, j] + " "); } Console.WriteLine(); } } } }