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

  • Write 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.

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

/*
* C Program to calculate area of a 
* any triangle
*/
#include <stdio.h>
#include <math.h>
#include <conio.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);
    /* Area of any triangle = 
       sqrt(s*(s-sideOne)*(s-sideTwo)*(s-sideThree))
       Where s = (sideOne + sideTwo + sideThree)/2  */
    s = (sideOne + sideTwo + sideThree)/2;
    area = sqrt(s*(s-sideOne)*(s-sideTwo)*(s-sideThree));
    printf("Area of triangle : %0.4f\n", area);
     
    getch();
    return 0;
}

Program 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.

Test yourself:

  1. C Program to Calculate Area of Any Triangle using Heron’s Formula
  2. C Program To Calculate Area Of Triangle Using Heron’s Formula
  3. Write A Program To Find Area Of Triangle Using Heron’s Formula
  4. Program To Find Area Of Triangle Using Heron’s Formula In C
  5. C Program To Find Area Of Triangle
  6. C Program To Calculate Area Of Triangle
  7. C Program To Find The Area Of A Triangle
  8. Write A C Program To Find Area Of Triangle
  9. Find The Area Of A Triangle In C Program
  10. Area Of Triangle Program In C
  11. C Program For Heron’s Formula
  12. C Program To Find Area Of Triangle Using Heron’s Formula
  13. Area Of Triangle Using Heron’s Formula In C
  14. Area Of Circle Formula In C Program
  15. Heron’s Formula In C
  16. C Program Area Of Triangle
  17. C Program For Area Of Triangle
  18. Area Of Triangle C Program
  19. Area Of Triangle In C Program
  20. Area Of Triangle In C
  21. Heron’s Formula In Python
  22. Heron’s Formula C Program
  23. Area Of Triangle Using Heron’s Formula
  24. Program Of Area Of Triangle In C
  25. Area Of Circle Formula C Program
  26. Area Using Heron’s Formula

Recommended Reading On: Python Program to Calculate the Area, Semi Perimeter and Perimeter of a Triangle