Example 44: Area of a Triangle

Learn how to calculate the area of a triangle using pointers.


Write a function to compute the distance between two points, and use it to develop another function that will compute the area of the triangle whose vertices are A (x1, y1), B (x2, y2), and C (x3, y3), and ascertain if the triangle is valid.


Input (Point 1, Point 2, Point 3) Output
0 0
6 0
0 8
Length of first side = 6.000000
Length of second side = 10.000000
Length of third side = 8.000000
Area: 24.000000
0 4
0 8
0 15
Length of first side = 4.000000
Length of second side = 7.000000
Length of third side = 11.000000
Not a triangle

Try it yourself

Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Note: To run your solution or our given solution, follow the following steps:

  • Click on the run button.
  • Wait for the 4 steps to complete, and then switch to the terminal tab.
  • In the terminal tab, write the following command:
    gcc main.c -o main -lm
    Then, write this command:

Note: The C language requires -lm flag while executing a program that uses the math library.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.