Darshana Salaskar - 6 months ago 26

Java Question

Though a triangle is a simple structure with three lines, it is not possible for any three positive integers to be the line lengths. It is because no side can be either equal to or greater than the sum of other two sides.

Write a Java program that would take three integers and print the diagnostic that the three sides can form a triangle, or a straight line or cannot form a triangle or straight line. (When the sum of the two lengths is equal to the third length, then a straight line is formed)

Sample input

2 4 5

Sample output

2 4 5 can form a triangle

Sample input

1 2 4

Sample output

1 2 4 cannot form a triangle or straight line

Sample input

1 2 3

Sample output

1 2 3 can form a straight line

Answer

Here you are :-

```
import java.util.Scanner;
public class Distance {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double x1=0;
double x2=0;
double y1=0;
double y2=0;
double distance=0;
System.out.println("Enter the value of x1 : ");
x1=in.nextDouble();
System.out.println("Enter the value of y1 : ");
y1=in.nextDouble();
System.out.println("Enter the value of x2 : ");
x2=in.nextDouble();
System.out.println("Enter the value of y2 : ");
y2=in.nextDouble();
distance=Math.sqrt(Math.pow((x2-x1), 2)+Math.pow((y2-y1), 2));
System.out.printf("The distance between given coordinates is : %.5f",distance);
}
}
```

Do not forget to accept my answer by clicking right mark on top left corner of it. Happy Coding :)

Source (Stackoverflow)

Comments