15. JAVA Program to find the Distance between Two Points.
How does this program work?
- In this programme you will learn about how to measure the distance between two points using x,y Coordinates.
- First we need to store user entered values in x1,y1 and x2,y2.
- After that by using the given formula we can get the distance between two points using the below java program.
Here is the code
//Distance between Two Points
import java.util.Scanner;public class Distance
{
public static void main(String arg[])
{
int x1,x2,y1,y2,x,y;
double distance;
System.out.println("Enter x1 point" );
Scanner obj=new Scanner(System.in);
x1=obj.nextInt();
System.out.println("Enter x2 point" );
x2=obj.nextInt();
System.out.println("Enter y1 point");
y1=obj.nextInt();
System.out.println("Enter y2 point");
y2=obj.nextInt();
x=x2-x1;
y=y2-y1;
//Distance formula
distance=Math.sqrt((x*x )+ (y*y));System.out.println("Distance:"+"("+x1+","+x2+"), "+"("+y1+","+y2+")===>"+distance);
}
}