C Online Compiler
Example: Determinant of a 3x3 Matrix in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Determinant of a 3x3 Matrix #include <stdio.h> int main() { // Step 1: Declare a 3x3 matrix int matrix[3][3]; int i, j; long determinant; // Using long for determinant to handle larger values // Step 2: Prompt the user to enter elements of the 3x3 matrix printf("Enter the elements of the 3x3 matrix:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("Enter element matrix[%d][%d]: ", i, j); scanf("%d", &matrix[i][j]); } } // Step 3: Display the entered matrix (optional, for verification) printf("\nThe entered 3x3 matrix is:\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d\t", matrix[i][j]); } printf("\n"); } // Step 4: Calculate the determinant using the cofactor expansion method // Formula: det = a(ei - fh) - b(di - fg) + c(dh - eg) // Here, a = matrix[0][0], b = matrix[0][1], c = matrix[0][2] // d = matrix[1][0], e = matrix[1][1], f = matrix[1][2] // g = matrix[2][0], h = matrix[2][1], i = matrix[2][2] determinant = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1]) - matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]) + matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]); // Step 5: Print the calculated determinant printf("\nDeterminant of the 3x3 matrix = %ld\n", determinant); return 0; }
Output
Clear
ADVERTISEMENTS