October 19, 2021, Learn eTutorial

914

Here we are explaining how to write a C program to find the area of a triangle with three sides using heron's formula. For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

**The area of a Triangle is the region that comes inside the three sides of the triangle** that makes a closed shape.

Triangles can be classified into different types by following shapes and properties, important among them are the **Equilateral triangle**, **Isosceles triangle**, **Right-angled Triangle**.

In this C program, we are considering a normal Triangle with three different length sides and the length of each side is known for finding its Area. We are using **Heron's formula** to find such a triangle's area.

Heron's formula can be applied in this C program using two different steps, which are

- First, we have to find the perimeter "
**s**" of the triangle which can be found by adding the length of three sides of the triangle. - Second, we have to apply that perimeter value '
**s**' in the Herons formula with the sides of the triangle to find the area. **s = (a+b+c)/2**

where **a**,**b**,**c** are the three sides of the triangle and '**s**' is the perimeter.

- We are defining each variable using the
`int`

datatype and taking the user's input to get the Triangle's sides' values using the`printf`

and`scanf`

function defined in the standard libraries. - Now we have to compute the value of perimeter '
**s**' by adding the sides of the triangle and dividing it by 2. - Finally, apply the value of perimeter '
**s**' in the heron's formula to find the area of the triangle using sqrt function and print the result.

**STEP 1**: Include the header files used in C programming to use some built-in functions inside the program such as `stdio.h`

, `conio.h`

, `math.h`

**STEP 2**: Open the **main()** to start the program, C program execution starts with the **main()**

**STEP 3**: Accept the Sides of the Triangle from the user using `printf`

and `scanf`

built-in functions and store the values in Variables.

**STEP 4**: Calculate the value of **S** using the formula **a+b+c /2**

**STEP 5**: Calculate the area using the formula **sqrt[s(s-a)*(s-b)*(s-c)] **and assigned to variable** area**

**STEP 6**: Using the `printf`

method print the value of the area in C programming.

` ````
#include <stdio.h>
#include <math.h>
void main() {
int s, a, b, c, area; // defines the variables.
printf("Enter the values of a,b and c\n");
scanf("%d %d %d", & a, & b, & c);
/* compute s*/
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area of a triangle = %d\n", area);
}
```

Enter the values of a,b and c 3 4 5 Area of a triangle = 6