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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework