w3resource
C# Sharp Programming Exercises

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

C# Sharp Array: Exercise-28 with Solution

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

Sample Solution:-

C# Sharp Code:

using System;  
public class Exercise28 
{  
    public static void Main()
   {
            int i,j;
        	int[,] arr1 = new int[10,10];
            int det=0;
  
  
       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++)
        {
            for(j=0;j<3;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
			   arr1[i,j] = Convert.ToInt32(Console.ReadLine());
            }
        }  
	 Console.Write("The matrix is :\n");
	 for(i=0;i<3;i++)
	 {
	   for(j=0;j<3 ;j++)
	     Console.Write("{0}  ",arr1[i,j]);
	    Console.Write("\n");
	 }

  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]));

  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?



New Content: Composer: Dependency manager for PHP, R Programming