164k views
1 vote
Write a program that reads the lengths of the sides of a triangle from the user. Compute the area of the triangle using Heron's formula (below), in which s represents half of the perimeter of the triangle and a, b, and c represent the lengths of the three sides. Print rhe area to three decimal places.

Area= √ s(s-a)(s-b)(s-c)

User Glasses
by
3.5k points

2 Answers

1 vote

Final answer:

To calculate the area of a triangle using Heron's formula, you need to determine the lengths of the sides of the triangle. Once you have the side lengths, you can apply the formula. Let's say the lengths of the sides are a = 5, b = 7, and c = 9. The area of the triangle is approximately 16.46.

Step-by-step explanation:

To calculate the area of a triangle using Heron's formula, we need to first determine the lengths of the three sides of the triangle. Once we have the side lengths, we can apply the formula:

A = √(s(s-a)(s-b)(s-c))

Where s is half the perimeter of the triangle, and a, b, and c are the lengths of the three sides.

Let's say the lengths of the sides of the triangle are a = 5, b = 7, and c = 9. We can calculate the area as follows:

s = (a + b + c) / 2 = (5 + 7 + 9) / 2 = 21 / 2 = 10.5

Now, substitute the values into the formula:

A = √(10.5(10.5-5)(10.5-7)(10.5-9))

A = √(10.5 * 5.5 * 3.5 * 1.5)

A = √(270.5625)

A ≈ 16.46

User Guneykayim
by
2.4k points
1 vote

Answer:

The java program is as follows.

import java.util.Scanner;

import java.lang.*;

public class Area

{

//variables to hold the values

static double a, b, c;

static double s;

static double area;

public static void main(String[] args) {

//scanner class object created

Scanner sc = new Scanner(System.in);

System.out.print("Enter the first side: ");

a=sc.nextInt();

System.out.print("Enter the second side: ");

b=sc.nextInt();

System.out.print("Enter the third side: ");

c=sc.nextInt();

s=(a+b+c)/2;

//function of Math class used

area = Math.sqrt( s*(s-a)*(s-b)*(s-c) );

//result displayed with 3 decimal places

System.out.printf("The area of the triangle is %.3f", area);

}

}

OUTPUT

Enter the first side: 1

Enter the second side: 1

Enter the third side: 1

The area of the triangle is 0.433

Step-by-step explanation:

1. The variables to declare the three sides of the triangle, the semi-perimeter and the area of the triangle are declared with double datatype. All the variables are declared at class level and hence, declared with keyword, static.

2. Inside main(), an object of the Scanner class is created.

Scanner sc = new Scanner(System.in);

3. Using the Scanner class object, user input is taken for all the three sides of the triangle.

4. Following this, the semi-perimeter is computed as shown.

s=(a+b+c)/2;

5. The area of the triangle is computed using the given formula which is implemented as shown.

area = Math.sqrt( s*(s-a)*(s-b)*(s-c) );

6. The sqrt() method of the Math class is used while computing the area of the triangle.

7. The area is displayed with three decimals. This is done using the printf() method as shown.

System.out.printf("The area of the triangle is %.3f", area);

8. The program is saved with the same name as the name of the class having the main() method.

9. The class having the main() method is always public.

User Mark Ewer
by
4.0k points