Monday 23 March 2020

AREA OF A TRIANGLE USING JAVA LANGUAGE

Q-23-Write a java 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 area of a triangle is

    s = (side1 + side2 + side3)/2;
    𝑎𝑟𝑒𝑎 = √𝑠 ∗ (𝑠 − 𝑎) ∗ (𝑠 − 𝑏) ∗ (𝑠 − 𝑐)

INPUT :
import java.util.Scanner;
public class NCM {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double x1,y1,x2,y2,x3,y3;
System.out.println("Enter The Points For A Triangle :");
x1=sc.nextDouble();
y1=sc.nextDouble();
x2=sc.nextDouble();
y2=sc.nextDouble();
x3=sc.nextDouble();
y3=sc.nextDouble();
double a=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
double side1=Math.pow(a, 0.5);
double b=(x3-x2)*(x3-x2)+(y3-y2)*(y3-y2);
double side2=Math.pow(b, 0.5);
double c=(x1-x3)*(x1-x3)+(y1-y3)*(y1-y3);
double side3=Math.pow(c, 0.5);
double s=(side1+side2+side3)/2;
double d=s*(s-side1)*(s-side2)*(s-side3);
double area=Math.pow(d, 0.5);
System.out.println("The Area Of The Triangle Is :"+area);
}


}

OUTPUT :
Enter The Points For A Triangle :
1.5
-3.4
4.6
5
9.5
-3.4

The Area Of The Triangle Is :33.600000000000016

No comments:

Post a Comment