89.9k views
3 votes
Write a program that prompt the user to enter three points (x1, y1), (x2, y2), (x3,y3) of a triangle and displays its area.

1 Answer

1 vote

Answer:

Here is code in java.

import java.util.*;

class Main

{

public static void main (String[] args) throws java.lang.Exception

{

try{

//variables which store coordinates of all points

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

// scanner object to read input from user

Scanner scan = new Scanner(System.in);

System.out.println("Enter the coordinate of first point (x1,y1):");

// read coordinate of first point

x1=scan.nextFloat();

y1=scan.nextFloat();

System.out.println("Enter the coordinate of second point (x2,y2):");

// read coordinate of second point

x2=scan.nextFloat();

y2=scan.nextFloat();

System.out.println("Enter the coordinate of third point (x3,y3):");

// read coordinate of third point

x3=scan.nextFloat();

y3=scan.nextFloat();

//calculating area of the triangle

float area_tri=Math.abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2);

System.out.println("area of the triangle:"+area_tri);

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Declare six variables x1,x2,x3,y1,y2,y3 to store the coordinate of all three points of circle.Read the coordinate of all points and assign it to (x1,y1) for First point, (x2,y2) for second point and (x3,y3) for third point from user.Calculate area of triangle with the help of mentioned formula area=abs((x1*(y2-y3)+x2*(y3-y1)+x3*(y1-y2))/2).

Output:

Enter the coordinate of first point (x1,y1):

0 0

Enter the coordinate of second point (x2,y2):

2 9

Enter the coordinate of third point (x3,y3):

5 5

area of the triangle:17.5

User Maxine
by
5.3k points