# Heron's Formula

• February 5th, 2013, 12:39 PM
guneet.oct10@gmail.com
Heron's Formula
Heyy friendss!!
nd tell me what is the correction.. I'm always getting the area as 0 ... so help me!
Thanxx!

class heron
{
public static void main(String args[])
{
int a=6,b=4,c=10;
int s= (a+b+c) / 2;
double area = Math.sqrt(s*(s-a) * (s-b) * (s-c));
System.out.println("Program to calculate the Area of a Triangle using Heron's Formula");
System.out.println(" ");
System.out.println("The First side of the Triangle is " +a);
System.out.println("The Second side of the Triangle is "+b);
System.out.println("The Third side of the Triangle is " +c);
System.out.println("The Area of the Triangle is " + area);
}
}
• February 5th, 2013, 01:17 PM
Norm
Re: Heron's Formula
What does the program print out when it executes? Please copy the full output and paste it here.
What is the correct answer? Have you used a calculator to verify the code has the right equation?

ON windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

[code=java]
[/code]
to get highlighting and preserve formatting.
• February 5th, 2013, 01:18 PM
StormyWaters
Re: Heron's Formula
What are the values you are seeing in each step of the area calculation? If you were doing this by hand, what would you get?
• February 6th, 2013, 07:24 AM
Zaphod_b
Re: Heron's Formula
Well...

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.

Code java:

```  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.

However...

Not every set of numbers a, b, 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, b, c must be met so that they can be the sides of a triangle?

Cheers!

Z