C Program to Calculate Area of Any Triangle using Heron's Formula

Here is a C program to find the area of a triangle using Heron's Formula. If we know the length of all sides of any triangle, then we can calculate the area of triangle using Heron's Formula. Heron's formula is a generic formula and is not specific to any triangle, it can be used it find area of any triangle whether it is right triangle, equilateral triangle or scalene triangle. Heron's formula relates the side lengths, perimeter and area of a triangle.

Heron's formula(also known as Hero's formula) is named after Hero of Alexandria a Greek Engineer and Mathematician. Heron was a great mathematicians of his time and came up with this formula in the first century BC. He also expanded the scope of this formula to calculate the area of quadrilaterals and polygons.

Steps to find the area of a triangle using Heron's formula
Let A, B and C be the length of three sides of a triangle.
  • Calculate the semi perimeter of the triangle.
    Semi-Perimeter of triangle(S) = (A + B + C)/2
  • Now, we can calculate the area of triangle using below mentioned formula.
    Area of Triangle = √ S(S-A)(S-B)(S-C)) 
    Where, S is the semi-perimeter that we calculated in first step.
Example

Let ABC be a triangle, whose length of sides are 5, 10 and 7 meters. To calculate the area of this triangle first of all we should calculate it's semi-perimeter.
Semi-Perimeter(S) = (5+10+7)/2 = 11
Now, we can calculate area of triangle ABC using Heron's formula
Area = √ 11(11-5)(11-10)(11-7))  = √ 264  = 16.24 m2


C Program to find the area of a triangle using Heron's formula

To calculate the area of triangle using Heron's formula we need length of all three sides of a triangle. Below program first takes the length of three sides of a triangle as input from user using scanf function and stores them in three floating point variable "sideOne", "sideTwo" and "sideThree".

In line number 16, it calculates the semi-perimeter of triangle as mentioned above and stores it in a floating point variable 's'. In line number 17, it calculates the area of triangle using the heron's formula given above and stores the area in a floating point variable 'area'. At last, it prints the area of triangle on screen using printf function.

#include <stdio.h>
#include <math.h>

int main(){
    float sideOne, sideTwo, sideThree, s, area;
    
    printf("Enter the length of three sides of 
        triangle\n");
    scanf("%f %f %f", &sideOne, 
        &sideTwo, &sideThree);

    s = (sideOne + sideTwo + sideThree)/2;
    area = sqrt(s*(s-sideOne)*(s-sideTwo)*(s-sideThree));
    printf("Area of triangle : %0.4f\n", area);
    
    return 0;
}
Output
Enter the length of three sides of triangle
3 4 5
Area of triangle : 6.0000
Enter the length of three sides of triangle
2 2 4
Area of triangle : 0.0000

Heron's formula is also useful in solving problems in which you know the area of a triangle and length of two sides of a triangle and want to calculate the length of third side of a triangle.


Related Topics
C program to calculate area of an equilateral triangle
C program to calculate area of a right angled triangle
C program to calculate volume and total surface area of cone
C program to calculate volume and total surface area of cylinder
C program to calculate volume and total surface area of cube
C program to calculate area of a trapezium
C program to calculate area of a parallelogram
C program to calculate volume and total surface area of cuboid
List of all C Programs