Menu Close

Distance of two points (x, y) in Java ..fcukthecode

Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them.

Use Double Data Type and String.format(” %.2f”, outputValue);

INPUT
2  3  4  5

OUTPUT
2.83
import java.util.*;
import java.io.*;
import java.lang.Math;
public class temp {
	 public static void main(String[] args) {
       double x1,y1,x2,y2,outputValue;
       Scanner scanobj = new Scanner(System.in);
       System.out.println("Enter the first point(x1,y1):  ");x1 = scanobj.nextDouble();
       y1 = scanobj.nextDouble();
       System.out.println("Enter the second point(x2,y2):  ");
       x2 = scanobj.nextDouble();
       y2 = scanobj.nextDouble();
       outputValue = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
       System.out.println("\nDistance between the two point is:  "+String.format("%.2f",outputValue));
	}
}

INPUT_1:
Enter the first point(x1,y1):
2  3
Enter the second point(x2,y2):
4  5

OUTPUT:
Distance between the two point is:  2.83


INPUT_2:
Enter the first point(x1,y1):
2  1
Enter the second point(x2,y2):
3  4

OUTPUT:
Distance between the two point is:  3.16


INPUT_3:
Enter the first point(x1,y1):
56  65
Enter the second point(x2,y2):
23  44

OUTPUT:
Distance between the two point is:  39.12


INPUT_4:
Enter the first point(x1,y1):
100  120
Enter the second point(x2,y2):
200  250

OUTPUT:
Distance between the two point is:  164.01


INPUT_5:
Enter the first point(x1,y1):
89  56
Enter the second point(x2,y2):
89  70

OUTPUT:
Distance between the two point is: 14.00


INPUT_6:
Enter the first point(x1,y1):
60  5
Enter the second point(x2,y2):
81  10

OUTPUT:
Distance between the two point is:  21.59


ILLUSTRATION:

Executed using javac in terminal (linux)Ubuntu