Write a C program to read elements in a matrix and find determinant of the given matrix. C program to find determinant of a 2x2 matrix and 3x3 matrix.

1 2

3 4

Output: Determinant of the matrix = -2

Read more facts about determinants and formulas to calculate determinants on Math is Fun.

Happy coding ;)

**Example:**If elements of a 2x2 matrix are:1 2

3 4

Output: Determinant of the matrix = -2

### Required knowledge:

Basic C programming, For loop, Array, Matrix#### Determinant:

The Determinant of a matrix is a special number that can be calculated from the elements of a square matrix. The determinant of a matrix**A**is denoted by det (*A*), det*A*or |*A*|.Read more facts about determinants and formulas to calculate determinants on Math is Fun.

### Program to calculate determinant of 2x2 matrix:

/** * C program to find determinant of 2x2 matrix */ #include <stdio.h> int main() { int A[2][2]; int row, col; long det; /* * Reads elements in matrix A from user */ printf("Enter elements in matrix of size 2x2: \n"); for(row=0; row<2; row++) { for(col=0; col<2; col++) { scanf("%d", &A[row][col]); } } /* * det(A) = ad - bc * a = A[0][0], b = A[0][1], c = A[1][0], d = A[1][1] */ det = (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]); printf("Determinant of matrix A = %ld", det); return 0; }

Output

Enter elements in matrix of size 2x2:

1 2

3 4

Determinant of matrix A = -2

1 2

3 4

Determinant of matrix A = -2

### Program to calculate determinant of 3x3 matrix:

/** * C program to find determinant of 3x3 matrix */ #include <stdio.h> int main() { int A[3][3]; int row, col; int a, b, c, d, e, f, g, h, i; long det; /* * Reads elements in matrix A from user */ printf("Enter elements in matrix of size 3x3: \n"); for(row=0; row<3; row++) { for(col=0; col<3; col++) { scanf("%d", &A[row][col]); } } /* * Used as a temporary variables to make calculation easy * | | * | a b c | * | d e f | * | g h i | * | | */ a = A[0][0]; b = A[0][1]; c = A[0][2]; d = A[1][0]; e = A[1][1]; f = A[1][2]; g = A[2][0]; h = A[2][1]; i = A[2][2]; /* * det(A) = a(ei - fh) - b(di - fg) + c(dh - eg) */ det = (a*(e*i - f*h)) - (b*(d*i - f*g)) + (c*(d*h - e*g)); printf("Determinant of matrix A = %ld", det); return 0; }

**Note:**You can also calculate determinants without using additional temporary variables a, b, c, d, e, f, g, h, u. These variable are only used to make the program simple to use. You can directly use A[0][0] for a etc.

Output

Enter elements in matrix of size 3x3:

6 1 1

4 -2 5

2 8 7

Determinant of matrix A = -306

6 1 1

4 -2 5

2 8 7

Determinant of matrix A = -306

Happy coding ;)

### You may also like

- Array and Matrix programming exercises index.
- C program to find transpose of a matrix.
- C program to find upper triangular matrix.
- C program to find lower triangular matrix.
- C program to find sum of each row and columns of a matrix.
- C program to interchange diagonals of a matrix.
- C program to check whether two matrices are equal or not.
- C program to check Identity matrix.
- C program to check Sparse matrix.
- C program to check Symmetric matrix.
- C program to find reverse of an array.
- C program to delete all duplicate elements from array.
- C program to find HCF of two numbers.
- C program to find LCM of two numbers.
- C program to find sum of all Prime numbers between 1 to n.
- C program to print different star(*) pattern series.

< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)