In this example, we will see C Program to find Area of triangle when its 3 sides are given. Heron’s formula’s formula is used to write this program so you should have a prior knowledge about heron’s formula before preceding.
The method used to find the area of triangle when 3 sides are given is called Heron’s formula which is also a very popular formula.
Heron’s formula gives the area of a triangle when the length of all three sides are known. Unlike other triangle area formulae, there is no need to calculate angles or other distances in the triangle first.
It is a generic formula and also is not specific to any triangle, it can be used it find area of any triangle whether it is right, equilateral or scalene triangle. Heron’s formula relates the side lengths, perimeter and area of a triangle.
Heron’s formula states that the area of a triangle whose sides have lengths a, b, and c is
where s is the semi-perimeter of the triangle; that is,
Heron’s formula can also be written as
Program to find area of triangle using Heron’s Formula
We need length of all three sides of a triangle, to calculate the area of triangle using Heron’s formula .
The Program below first takes the length of three sides of a triangle as input from user using the scanf function and then stores them in three floating(Float) point variables “a”, “b” and “c”.
Later then it calculates the semi-perimeter of triangle as mentioned above and stores it in a floating point variable ‘s’.
Then, it calculates the area of triangle using the heron’s formula and stores the area in a floating point variable ‘area’.
At last of the program, it prints the area of triangle on screen using the printf function.
/* C-Program Area of triangle when three sides are given */ #include <stdio.h> #include <math.h> int main() { float a,b,c, s,area; printf("Enter the side 1 of triangle: "); scanf("%f", &a); printf("Enter the side 2 of triangle: "); scanf("%f", &b); printf("Enter the side 3 of triangle: "); scanf("%f", &c); s= (a+b+c)/2; area= sqrt(s*(s-a)*(s-b)*(s-c)); printf("\n"); printf("side 1 = %f \n", a); printf("side 2 = %f \n", b); printf("side 3 = %f \n", c); printf("Area of Triangle = %.2f", area); //.2f is used to round off the digits to 2 decimal places return 0; }
Output :-
Enter the side 1 of triangle: 5.2 Enter the side 2 of triangle: 6.0 Enter the side 3 of triangle: 10.4 side 1 = 5.200000 side 2 = 6.000000 side 3 = 10.400000 Area of Triangle = 10.78