Write a python program to calculate distance between two points taking input from the user
Distance can be calculated using the two points (x_{1}, y_{1}) and (x_{2}, y_{2}), the distance d between these points is given by the formula:
for e.g : let x_{1 , }y_{1}=10,9 and x_{2} , y_{2}=4,1 then (x_{2}-x_{1})^{2}=(10-4)^{2 }= 6^{2 }= 36 and (y_{2}-y_{1})^{2}= (9-1)^{2} = 8^{2 }= 64 now 64 + 36 =100 and 100 is square root of 10 sp distance between (10,9) and (4,1) is 10 .
Program-1
x1=int(input("enter x1 : ")) x2=int(input("enter x2 : ")) y1=int(input("enter y1 : ")) y2=int(input("enter y2 : ")) result= ((((x2 - x1 )**2) + ((y2-y1)**2) )**0.5) print("distance between",(x1,x2),"and",(y1,y2),"is : ",result) |
---|
Output
enter x1 : 4
enter x2 : 6
enter y1 : 0
enter y2 : 6
distance between (4, 6) and (0, 6) is : 6.324555320336759
Program -2
import math a=input("enter first coordinate : ") p1 = a.split(",") b=input("enter second coordinate : ") p2 = b.split(",") distance = math.sqrt( ((int(p1[0])-int(p2[0]))**2)+((int(p1[1])-int(p2[1]))**2) ) print("distance between ", a,"and", b, "is",distance) |
---|
Output
enter first coordinate: 4,0
enter second coordinate : 6,6
distance between 4,0 and 6,6 is 6.324555320336759
For more Rajasthan Technical University CSE VI Sem Python Lab Experiments Click here