Monday 23 March 2020

DISTANCE BETWEEN TWO POINTS USING JAVA LANGUAGE

Q-22- Write a java program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them. The formula for computing the distance is √(๐‘ฅ2 − ๐‘ฅ1)2 + (๐‘ฆ2 − ๐‘ฆ1)2.  Note that you can use Math.pow (a, 0.5) to compute √๐‘Ž.

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,x2,y1,y2;
System.out.println("Enter x1 and y1:" );
x1=sc.nextDouble();
y1=sc.nextDouble();
System.out.println("Enter x2 and y2:" );
x2=sc.nextDouble();
y2=sc.nextDouble();
double p=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
double distance=Math.pow(p,0.5);
System.out.println("The Distance Between The Two Points is : "+distance);

}


}

OUTPUT :
Enter x1 and y1:
1.5
-3.4
Enter x2 and y2:
4
5

The Distance Between The Two Points is : 8.764131445842194

No comments:

Post a Comment