# C Program to Find the Area of a Triangle Using Determinants

«
»
This is a C program to find the area of triangle using determinants.
Formula for the area of a triangle using determinants
x1 y1 1
Area=±1/2 x2 y2 1
x3 y3 1
The plus/minus in this case is meant to take whichever sign is needed so the answer is positive (non-negative). Do not say the area is both positive and negative.

Here is source code of the C Program to Compute the Area of a Triangle Using Determinants. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include <string.h>`
2. `#include <stdio.h>`
3. `#include <stdlib.h>`
4. ` `
5. `int i, j, c;`
6. `double det(int n, double mat) {`
7. `    double submat;`
8. `    float d;`
9. `    for (c = 0; c < n; c++) {`
10. `        int subi = 0; //submatrix's i value`
11. `        for (i = 1; i < n; i++) {`
12. `            int subj = 0;`
13. `            for (j = 0; j < n; j++) {`
14. `                if (j == c)`
15. `                    continue;`
16. `                submat[subi][subj] = mat[i][j];`
17. `                subj++;`
18. `            }`
19. `            subi++;`
20. ` `
21. `        }`
22. `        d = d + (pow(-1, c) * mat[c] * det(n - 1, submat));`
23. `    }`
24. `    return d;`
25. `}`
26. `int main(int argc, char **argv) {`
27. ` `
28. `    printf("Enter the points of the triangle:\n");`
29. `    int x1, x2, x3, y1, y2, y3;`
30. `    scanf("%d", &x1);`
31. `    scanf("%d", &y1);`
32. ` `
33. `    scanf("%d", &x2);`
34. `    scanf("%d", &y2);`
35. ` `
36. `    scanf("%d", &x3);`
37. `    scanf("%d", &y3);`
38. ` `
39. `    double mat;`
40. `    mat = x1;`
41. `    mat = y1;`
42. `    mat = 1;`
43. `    mat = x2;`
44. `    mat = y2;`
45. `    mat = 1;`
46. `    mat = x3;`
47. `    mat = y3;`
48. `    mat = 1;`
49. ` `
50. `    printf("\nMatrix formed by the points: \n");`
51. `    for (i = 0; i < 3; i++) {`
52. `        for (j = 0; j < 3; j++) {`
53. `            printf("%lf ", mat[i][j]);`
54. `        }`
55. `        printf("\n");`
56. `    }`
57. ` `
58. `    float determinant = det(3, mat) * 0.5;`
59. `    if (determinant < 0)`
60. `        printf(`
61. `                "The area of triangle formed by (%d, %d), (%d, %d), (%d, %d) = %lf ",`
62. `                x1, y1, x2, y2, x3, y3, (determinant * -1));`
63. `    else`
64. `        printf(`
65. `                "The area of triangle formed by (%d, %d), (%d, %d), (%d, %d) = %lf ",`
66. `                x1, y1, x2, y2, x3, y3, (determinant));`
67. `    return 0;`
68. `}`

Output:

```\$ gcc TraingleArea.c
\$ ./a.out

Enter the points of the triangle:
3 4
6 4
3 9

Matrix formed by the points:
3 4 1
6 4 1
3 9 1
The Area of the triangle formed by (3,4), (6,4), (3,9) = 7.5```

Sanfoundry Global Education & Learning Series – 1000 C Programs. 