w3resource

C#: Calculate the determinant of a 3 x 3 matrix

C# Sharp Array: Exercise-28 with Solution

Write a C# Sharp program to calculate the determinant of a 3 x 3 matrix.

Sample Solution:-

C# Sharp Code:

using System;

public class Exercise28 
{  
    public static void Main()
    {
        // Declaration of variables for matrix manipulation
        int i, j;
        int[,] arr1 = new int[10, 10]; // Declare a 10x10 integer matrix
        int det = 0; // Initialize determinant value to zero

        // User input for matrix elements
        Console.Write("\n\nCalculate the determinant of a 3 x 3 matrix :\n");
        Console.Write("-------------------------------------------------\n");  
        Console.Write("Input elements in the matrix :\n");
        for (i = 0; i < 3; i++) // Loop through rows
        {
            for (j = 0; j < 3; j++) // Loop through columns
            {
                // Prompt user to enter matrix element
                Console.Write("element - [{0}],[{1}] : ", i, j);
                arr1[i, j] = Convert.ToInt32(Console.ReadLine()); // Read user input and store in the matrix
            }
        }  

        // Display the entered matrix
        Console.Write("The matrix is :\n");
        for (i = 0; i < 3; i++) // Loop through rows
        {
            for (j = 0; j < 3; j++) // Loop through columns
                Console.Write("{0}  ", arr1[i, j]); // Print each element of the matrix
            Console.Write("\n"); // Move to the next row
        }

        // Calculate the determinant of the 3x3 matrix using the method for a 3x3 matrix
        for (i = 0; i < 3; i++)
            det = det + (arr1[0, i] * (arr1[1, (i + 1) % 3] * arr1[2, (i + 2) % 3] - arr1[1, (i + 2) % 3] * arr1[2, (i + 1) % 3]));

        // Display the determinant of the matrix
        Console.Write("\nThe Determinant of the matrix is: {0}\n\n", det);
    }
}

Sample Output:

Calculate the determinant of a 3 x 3 matrix :                                                                 
-------------------------------------------------                                                             
Input elements in the matrix :                                                                                
element - [0],[0] : 2                                                                                         
element - [0],[1] : 6                                                                                         
element - [0],[2] : 8                                                                                         
element - [1],[0] : 10                                                                                        
element - [1],[1] : 12                                                                                        
element - [1],[2] : 14                                                                                        
element - [2],[0] : 16                                                                                        
element - [2],[1] : 18                                                                                        
element - [2],[2] : 20                                                                                        
The matrix is :                                                                                               
2  6  8                                                                                                       
10  12  14                                                                                                    
16  18  20                                                                                                    
                                                                                                              
The Determinant of the matrix is: 24

Flowchart:

Flowchart: Calculate the determinant of a 3 x 3 matrix

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to print or display upper triangular matrix.
Next: Write a program in C# Sharp to accept a matrix and determine whether it is a sparse matrix.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.