In this program you will learn calculating n x n determinant of a matrix in C#.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
class Program { public static double det(double[,] A, int N) { double c, r = 1; for (int i = 0; i < N; i++) { for (int k = i + 1; k < N; k++) { c = A[k, i] / A[i, i]; for (int j = i; j < N; j++) A[k, j] = A[k, j] - c * A[i, j]; } } for (int i = 0; i < N; i++) r *= A[i, i]; return r; } static void Main(string[] args) { Console.Write("Enter N number :"); int d = Convert.ToInt32(Console.ReadLine()); double[,] M = new double[d, d]; Random rnd = new Random(); //Print Matrix for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { M[i, j] = rnd.Next(5); //add numbers to matrix Console.Write(M[i, j] + " "); } Console.WriteLine(); } Console.WriteLine("Det(M) = {0}", det(M, d)); //calculate determinant Console.ReadLine(); } } |