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
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);
}
}
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