Question

Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3,...

Write a program that prompts the user to enter three points (x1,

y1), (x2, y2), (x3, y3) of a triangle and displays its area.

The formula for computing the distance of two points (x1, y1) and (x2, y2) is

d = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

or

d = Math.pow((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1), 0.5);

The formula for computing the area of a triangle is

s = (side1 + side2 + side3) / 2.0;

area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

or

area = Math.pow(s * (s - side1) * (s - side2) * (s - side3), 0.5);

Here is a sample run:

Enter three points for a triangle: 1.5 -3.4 4.6 5.0 9.5 -3.4

The area of the triangle is 33.60.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since you have not provided the language of your preference, I am providing the code in Java.

import java.util.Scanner;

public class tester {

   public static double getDistance(double x1, double y1, double x2, double y2) {

       return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

   }

  

  

   public static void main(String[] args) {

       double x1, y1, x2, y2, x3, y3;

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter three points for a triangle: ");

       x1 = sc.nextDouble();

       y1 = sc.nextDouble();

      

       x2 = sc.nextDouble();

       y2 = sc.nextDouble();

      

       x3 = sc.nextDouble();

       y3 = sc.nextDouble();

      

       double side1 = getDistance(x1, y1, x2, y2);

       double side2 = getDistance(x2, y2, x3, y3);

       double side3 = getDistance(x3, y3, x1, y1);

      

       double s = (side1 + side2 + side3) / 2.0;

       double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

      

       System.out.format("The area of the triangle is : %.2f", area);

      

      

   }

}

Add a comment
Know the answer?
Add Answer to:
Write a program that prompts the user to enter three points (x1, y1), (x2, y2), (x3,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT