Thursday, June 19, 2014

Elementary Programming; Exercise No. 13

 Write a 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 square root of [(x2 - x1 )2 + (y2 - y1 )2 ].

Solution:

public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   double x1, y1, x2, y2, distance;

   System.out.print("Enter x1 and y1:");
   x1 = s.nextDouble();
   y1 = s.nextDouble();

   System.out.print("Enter x2 and y2:");
   x2 = s.nextDouble();
   y2 = s.nextDouble();

   distance = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), 0.5);

   System.out.print("The distance between the two points is " + distance);
}

No comments:

Post a Comment

Author