Basic Programming C# Examples

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below:

using System;

namespace MatrixDeterminant
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of rows and columns of the matrix below to find the determinant of matrix:");
            int rows = int.Parse(Console.ReadLine());
            int columns = int.Parse(Console.ReadLine());

            int[,] matrix = new int[rows, columns];

            Console.WriteLine("Enter the elements of the matrix:");
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    matrix[i, j] = int.Parse(Console.ReadLine());
                }
            }

            int determinant = GetDeterminant(matrix, rows);

            Console.WriteLine("Okay, the determinant of the matrix is: " + determinant);
        }

        static int GetDeterminant(int[,] matrix, int n)
        {
            int det = 0;
            if (n == 1)
            {
                return matrix[0, 0];
            }
            int[,] subMatrix = new int[n - 1, n - 1];

            int sign = 1;
            for (int i = 0; i < n; i++)
            {
                GetSubMatrix(matrix, subMatrix, 0, i, n);
                det += sign * matrix[0, i] * GetDeterminant(subMatrix, n - 1);
                sign = -sign;
            }

            return det;
        }

        static void GetSubMatrix(int[,] matrix, int[,] subMatrix, 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)
                    {
                        subMatrix[i, j++] = matrix[row, col];
                        if (j == n - 1)
                        {
                            j = 0;
                            i++;
                        }
                    }
                }
            }
        }
    }
}

Output:


Hello Codeauri Family, enter the number of rows and columns of the matrix below to find the determinant of matrix:
2
2
Enter the elements of the matrix:
198
234
111
456
Okay, the determinant of the matrix is: 64314

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. The first line using System; is a directive that includes the System namespace in the code, which provides access to various types and methods, including the Console class.
  2. The code is inside a namespace MatrixDeterminant, which acts as a container for the class Program and other types and classes.
  3. The Program class contains the Main method, which is the entry point of the program.
  4. In the Main method, the user is prompted to enter the number of rows and columns of the matrix. The int.Parse method is used to parse the input and convert it to an integer.
  5. The matrix 2D array is declared and initialized using the number of rows and columns entered by the user.
  6. The user is then prompted to enter the elements of the matrix, which are stored in the matrix array.
  7. The determinant of the matrix is calculated by calling the GetDeterminant method, passing matrix and rows as arguments.
  8. The GetDeterminant method calculates the determinant of the matrix using recursion. It checks if n is equal to 1, in which case it returns the first element of the matrix. If n is not equal to 1, it calculates the determinant of submatrices. It declares a subMatrix 2D array to store the submatrix and calls the GetSubMatrix method to obtain the submatrix. The sign variable is used to keep track of the sign of each element in the calculation of the determinant. The determinant is calculated by adding the product of sign * matrix[0, i] * GetDeterminant(subMatrix, n - 1) for each i in 0 to n - 1.
  9. The GetSubMatrix method takes a 2D array matrix, a 2D array subMatrix, an integer p, an integer q, and an integer n as arguments and returns the submatrix of matrix with the pth row and qth column removed. It uses two variables i and j to keep track of the indices of subMatrix. The method iterates over the row from 0 to n - 1 and the col from 0 to n - 1, and if row is not equal to p and col is not equal to q, it stores the element in subMatrix. If j is equal to n - 1, j is set to 0 and i is incremented.
  10. Finally, the determinant is displayed on the console.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X