Distance Between 2 Places


Collapse Content

You're creating travel software, and need to calculate the distance between 2 locations located at (x1, y1) and (x2, y2) on a 2D grid. You will be given a list of doubles as input in the following order: x1, y1, x2, y2. Calculate and print the distance between the cities, rounded to 3 decimal places.

For example, given {0.0, 5.5, -1.0, 3.5} as input, calculate the distance between (0.0, 5.5) and (-1.0, 3.5), which is sqrt(5) ≃ 2.23607, so you should print 2.236

simple-graph-of-line

Challenge

Print the distance between 2 locations to 3 decimal locations.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • Isnt there an esier way to round up numbers to n decimal places?

    a solution:
    my code

  • We have to make a precision, for which there is no method? That's brutal.

  • I finally got the precision done, but only by copying and pasting a solution for that issue from Quora. It really would be helpful if we were led to a solution, sometimes. Right now I don't feel I'm learning from this.

  • In this case, Googling for how to round numbers would be good practice for real world programming.

Contact Us
Sign in or email us at [email protected]