Example 7: Distance Between the Two Points

Learn how to calculate the distance between two points on Earth.


Write a program that receives latitude values (L1, L2) and longitude (G1, G2) of two places on Earth, in degrees, and returns the distance between them.

The formula for distance in nautical miles is:

D = 3963 * acosacos ( sinL1sinL2\sin L1 * \sin L2 + cosL1cosL2\cos L1*\cos L2 * cos\cos( G2–G1 ))

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.