Calculates the distance between two points on the Earth's surface using the Haversine formula. It takes latitude and longitude coordinates for a starting point and an ending point as input and calculates the distance between them in kilometers
d = 6371.01 * acos(sin(slat) * sin(elat) + cos(slat) * cos(elat) * cos(slon - elon))
from math import radians, sin, cos, acos #latitude and longitude coordinates for the starting point slat = radians(float(input("Enter the Starting Latitude : "))) slon = radians(float(input("Enter the Starting Longitude : "))) #latitude and longitude coordinates for the ending point elat = radians(float(input("Enter the Ending Latitude : "))) elon = radians(float(input("Enter the Ending Longitude : "))) # Calculate the distance using the Haversine formula d = 6371.01 * acos(sin(slat) * sin(elat) + cos(slat) * cos(elat) * cos(slon - elon)) print("The distance is %.2f km" % d)
Starting latitude = 37.7749 Starting longitude = -122.4194 Ending latitude = 34.0522 Ending longitude = -118.2437 Distance = 559.12 km
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions