Try to draw a triangle with sides 6, 4, 10. (Graph paper and a compass will help here.)
The area is
zero, as the formula shows.
Change the values of the sides to 6, 4, 5. What do you get? Is this correct?
The correct answer is 9.921567416492215, as obtained from Online Conversion - Area of a scalene triangle
. There are about a million other places on line where you can check your work (maybe more).
Now if you don't get this value, go back and calculate each step by hand (and calculator). Put print statements in your program to see whether the values are the same as your hand calculations.
int a = 6, b = 4, c = 5;
int s = (a + b + c) / 2;
System.out.println("s = " + s);
System.out.println("s*(s-a)*(s-b)*(s-c) = " + s*(s-a)*(s-b)*(s-c));
double area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
Bottom line: When properly implemented, Heron's rule can find the area of a triangle.
Not every set of numbers a
, and c
can represent lengths of sides of a triangle. Maybe the program should include a check for validity before trying to compute the area.
What conditions on a
must be met so that they can be the sides of a triangle?