C Exercises: Calculate the determinant of a 3 x 3 matrix
C Array: Exercise-28 with Solution
Write a program in C to calculate the determinant of a 3 x 3 matrix.
Visual Presentation:

Sample Solution:
C Code:
#include <stdio.h>
int main() {
int arr1[10][10], i, j, n;
int det = 0;
// Display the purpose of the program
printf("\n\nCalculate the determinant of a 3 x 3 matrix :\n");
printf("-------------------------------------------------\n");
// Input elements into the matrix
printf("Input elements in the matrix :\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("element - [%d],[%d] : ", i, j);
scanf("%d", &arr1[i][j]);
}
}
// Display the matrix
printf("The matrix is :\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("% 4d", arr1[i][j]);
}
printf("\n");
}
// Calculate the determinant of the 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
printf("\nThe Determinant of the matrix is: %d\n\n", det);
return 0;
}
Sample Output:
Calculate the determinant of a 3 x 3 matrix : ------------------------------------------------- Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 0 element - [0],[2] : -1 element - [1],[0] : 0 element - [1],[1] : 0 element - [1],[2] : 1 element - [2],[0] : -1 element - [2],[1] : -1 element - [2],[2] : 0 The matrix is : 1 0 -1 0 0 1 -1 -1 0 The Determinant of the matrix is: 1
Flowchart:

C Programming Code Editor:
Previous: Write a program in C to print or display upper triangular matrix.
Next: Write a program in C 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.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join