Python Programing Lab Exercise - 2(a)

Aim: 
        Write a program to compute distance between two points taking input from the user  (Pythagorean Theorem).

Description: 
               The Pythagorean theorem is the basis for computing distance between two points. Let (x1,y1) and (x2,y2) be the co-ordinates of points on xy-plane. From Pythagorean theorem, the distance between two points is calculated using the formulae: To find the distance, we need to use the method sqrt(). This method is not accessible directly, so we need to import math module and then we need to call this method using math static object.

To find the power of a number, we need to use ** operator.

Algorithm:

Input: x1,y1,x2 and y2

Output: Distance between two points. 

Step1: Start 

Step2: Import math module 

Step3: Read the values of x1,y1,x2 and y2 

Step4: Calculate the distance using the formulae math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ) and store the result in distance 

Step5: Print distance 

Step6: Stop.

Program:


import math
x1=int(input("Enter number:"))
x2=int(input("Enter number:"))
y1=int(input("Enter number:"))
y2=int(input("Enter number:"))
distance=math.sqrt((x2-x1)**2+(y2-y1)**2)
print("Distance between two points is : ",distance)

                OR
import math;
x1=int(input("Enter x1--->"))
y1=int(input("Enter y1--->"))
x2=int(input("Enter x2--->"))
y2=int(input("Enter y2--->"))
d1 = (x2 - x1) * (x2 - x1);
d2 = (y2 - y1) * (y2 - y1);
res = math.sqrt(d1+d2)
print ("Distance between two points:",res);



Output:


1 comment

  1. Sir for same program the logic can be written as " d=(((x2-x1)**2)+((y2-y1)**2))**(1/2)"

    ReplyDelete

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...