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
where a,b,c are the three sides of the triangle and 's' is the perimeter.
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.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