Area of a Triangle (Heron’s Formula): flowchart, algorithm and program
This program reads the three sides of a triangle and finds its area using Heron's formula, so no height is needed. It first computes the semi-perimeter s = (a+b+c)/2 and then takes the square root of s(s-a)(s-b)(s-c).
▶ Watch the dry run step by step →
Algorithm
- Step 1: Start.
- Step 2: Read a, b, c.
- Step 3: Set s = (a + b + c) / 2.
- Step 4: Set area = sqrt(s(s-a)(s-b)(s-c)).
- Step 5: Print area.
- Step 6: Stop.
Pseudocode
BEGIN
READ a, b, c
s ← (a + b + c) / 2
area ← sqrt(s(s-a)(s-b)(s-c))
PRINT area
END
Program in C, Python and Java
Area of a Triangle (Heron’s Formula) program in C
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, s, area;
printf("Enter three sides: ");
scanf("%f %f %f", &a, &b, &c);
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));
printf("Area = %.2f\n", area);
return 0;
}Area of a Triangle (Heron’s Formula) program in Python
import math
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
s = (a + b + c) / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print("Area = %.2f" % area)Area of a Triangle (Heron’s Formula) program in Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three sides: ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
System.out.printf("Area = %.2f\n", area);
}
}
Sample input: a = 3, b = 4, c = 5
Output: Enter three sides: Area = 6.00
How it works
- scanf reads the three sides into a, b and c as floats, so decimal sides like 2.5 also work.
- s = (a + b + c) / 2 stores the semi-perimeter, which is half of the perimeter. For 3, 4, 5 this is 12 / 2 = 6.
- area = sqrt(s * (s - a) * (s - b) * (s - c)) multiplies the four values and takes the square root. For 3, 4, 5 that is sqrt(6 * 3 * 2 * 1) = sqrt(36) = 6.
- printf with %.2f prints the area rounded to 2 decimal places, so the output is Area = 6.00.
- math.h is needed for sqrt(). On some compilers you must link the math library with -lm.
Dry run (trace table)
| Step | Line | Box | a | b | c | s | area | Output |
|---|---|---|---|---|---|---|---|---|
| 1 | 4 | Start | ||||||
| 2 | 8 | Read a, b, c | 3 | 4 | 5 | Enter three sides: | ||
| 3 | 10 | s = (a + b + c) / 2 | 3 | 4 | 5 | 6 | ||
| 4 | 11 | area = sqrt(s(s-a)(s-b)(s-c)) | 3 | 4 | 5 | 6 | 6 | |
| 5 | 13 | Print area | 3 | 4 | 5 | 6 | 6 | Area = 6.00 |
| 6 | 14 | Stop | 3 | 4 | 5 | 6 | 6 |
Time and space complexity
Time: O(1) · Space: O(1). Only a fixed number of arithmetic operations are done on a fixed number of variables.
Common mistakes
- Writing s = a + b + c / 2 without brackets. Division happens first, so only c is halved and s is wrong.
- Forgetting #include <math.h> (or -lm when compiling with gcc), which causes errors or a wrong result from sqrt.
- Not checking that the sides form a valid triangle. If a + b <= c (or similar), the value inside sqrt is zero or negative, and the result is 0 or nan.
Viva questions
Q1. What is the semi-perimeter s, and how does line 10 calculate it?
The semi-perimeter is half the perimeter of the triangle. Line 10 adds the three sides and divides by 2, so s = (a + b + c) / 2.
Q2. Why are a, b, c, s and area declared as float and not int?
Sides can be decimal values, and s is often a fraction, for example 7/2 = 3.5. The area is rarely a whole number either. With int, the values would be truncated and the answer would be wrong.
Q3. Why is #include <math.h> needed in this program?
The program calls sqrt() on line 11, and its declaration is in math.h. Without the header the compiler does not know sqrt's prototype. On some systems you may also have to link the math library with -lm.
Q4. What does Heron's formula say, and where does line 11 use it?
Heron's formula gives the area as the square root of s(s-a)(s-b)(s-c). Line 11 multiplies those four terms and passes the product to sqrt().
Q5. What happens if the user enters sides like 1, 2 and 10, which cannot form a triangle?
Here s = 6.5, so s - c is negative and the product under the root is negative. sqrt() of a negative number returns NaN, so the program prints nan. The program has no validity check, and it should test that a+b>c, a+c>b and b+c>a before computing the area.
Q6. What is the output for edge inputs such as 0 0 0, or a degenerate triangle like 1 2 3?
For 0 0 0, s is 0 and the area is 0. For 1 2 3, s = 3, so s - c = 0 and the area is 0.00. Both are not real triangles, but the program prints 0 because it does not validate its input.
Q7. What is the time complexity of this program?
It is O(1), constant time. There are no loops or recursion, only a fixed number of arithmetic operations and one square root.
Q8. What would change if you declared the variables as double?
You would read them with %lf in scanf, and you would get more precision. float has only about 6 to 7 significant digits. The %.2f in printf would stay the same, since printf promotes float to double.
Frequently asked questions
What is Heron's formula?
Heron's formula gives the area of a triangle from its three sides: area = sqrt(s(s-a)(s-b)(s-c)), where s = (a+b+c)/2 is the semi-perimeter.
What happens if the three sides cannot form a triangle?
The product under the square root becomes negative (or zero), so sqrt returns nan (or 0). To handle it, check that a+b>c, b+c>a and a+c>b before calculating.
Why is the area for sides 3, 4, 5 equal to 6?
Here s = 6, so the area is sqrt(6*3*2*1) = sqrt(36) = 6. This matches the right triangle formula 1/2 * 3 * 4 = 6.