Write a program that prompts the user to enter the coordinates (x, y) of two points and then calculates the straight line distance between them. The required formula is
Solution
To simplify things, the terms (x1 – x2)2 and (y1 – y2)2 are calculated individually and the results are assigned to two temporary variables.
PHP
<?php echo "Enter coordinates for point A: "; $x1 = trim(fgets(STDIN)); $y1 = trim(fgets(STDIN)); echo "Enter coordinates for point B: "; $x2 = trim(fgets(STDIN)); $y2 = trim(fgets(STDIN)); $x_temp = pow($x1 - $x2, 2); $y_temp = pow($y1 - $y2, 2); $d = sqrt($x_temp + $y_temp); echo "Distance between points: ", $d; ?>
Java
public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader cin = new java.io. BufferedReader(new java.io.InputStreamReader(System.in)); double d, x1, x2, x_temp, y1, y2, y_temp; System.out.print("Enter coordinates for point A: "); x1 = Double.parseDouble(cin.readLine()); y1 = Double.parseDouble(cin.readLine()); System.out.print("Enter coordinates for point B: "); x2 = Double.parseDouble(cin.readLine()); y2 = Double.parseDouble(cin.readLine()); x_temp = Math.pow(x1 - x2, 2); y_temp = Math.pow(y1 - y2, 2); d = Math.sqrt(x_temp + y_temp); System.out.println("Distance between points: " + d); }
C++
#include <iostream> #include <cmath> using namespace std; int main() { double d, x1, x2, x_temp, y1, y2, y_temp; cout << "Enter coordinates for point A: "; cin >> x1; cin >> y1; cout << "Enter coordinates for point B: "; cin >> x2; cin >> y2; x_temp = pow(x1 - x2, 2); y_temp = pow(y1 - y2, 2); d = sqrt(x_temp + y_temp); cout << "Distance between points: " << d; return 0; }
C#
static void Main() { double d, x1, x2, x_temp, y1, y2, y_temp; Console.Write("Enter coordinates for point A: "); x1 = Double.Parse(Console.ReadLine()); y1 = Double.Parse(Console.ReadLine()); Console.Write("Enter coordinates for point B: "); x2 = Double.Parse(Console.ReadLine()); y2 = Double.Parse(Console.ReadLine()); x_temp = Math.Pow(x1 - x2, 2); y_temp = Math.Pow(y1 - y2, 2); d = Math.Sqrt(x_temp + y_temp); Console.Write("Distance between points: " + d); Console.ReadKey(); }
Visual Basic
Sub Main() Dim d, x1, x2, x_temp, y1, y2, y_temp As Double Console.Write("Enter coordinates for point A: ") x1 = Console.ReadLine() y1 = Console.ReadLine() Console.Write("Enter coordinates for point B: ") x2 = Console.ReadLine() y2 = Console.ReadLine() x_temp = (x1 - x2) ^ 2 y_temp = (y1 - y2) ^ 2 d = Math.Sqrt(x_temp + y_temp) Console.Write("Distance between points: " & d) Console.ReadKey() End Sub
Python
import math print("Enter coordinates for point A: ") x1 = float(input()) y1 = float(input()) print("Enter coordinates for point B: ") x2 = float(input()) y2 = float(input()) x_temp = (x1 - x2) ** 2 y_temp = (y1 - y2) ** 2 d = math.sqrt(x_temp + y_temp) print("Distance between points:", d)
or you can do it more quickly as shown here
PHP
<?php echo "Enter coordinates for point A: "; $x1 = trim(fgets(STDIN)); $y1 = trim(fgets(STDIN)); echo "Enter coordinates for point B: "; $x2 = trim(fgets(STDIN)); $y2 = trim(fgets(STDIN)); $d = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2)); echo "Distance between points: ", $d; ?>
Java
public static void main(String[] args) throws java.io.IOException { java.io.BufferedReader cin = new java.io. BufferedReader(new java.io.InputStreamReader(System.in)); double d, x1, x2, y1, y2; System.out.print("Enter coordinates for point A: "); x1 = Double.parseDouble(cin.readLine()); y1 = Double.parseDouble(cin.readLine()); System.out.print("Enter coordinates for point B: "); x2 = Double.parseDouble(cin.readLine()); y2 = Double.parseDouble(cin.readLine()); d = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); System.out.println("Distance between points: " + d); }
C++
#include <iostream> #include <cmath> using namespace std; int main() { double d, x1, x2, y1, y2; cout << "Enter coordinates for point A: "; cin >> x1; cin >> y1; cout << "Enter coordinates for point B: "; cin >> x2; cin >> y2; d = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2)); cout << "Distance between points: " << d; return 0; }
C#
static void Main() { double d, x1, x2, y1, y2; Console.Write("Enter coordinates for point A: "); x1 = Double.Parse(Console.ReadLine()); y1 = Double.Parse(Console.ReadLine()); Console.Write("Enter coordinates for point B: "); x2 = Double.Parse(Console.ReadLine()); y2 = Double.Parse(Console.ReadLine()); d = Math.Sqrt(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2)); Console.Write("Distance between points: " + d); Console.ReadKey(); }
Visual Basic
Sub Main() Dim d, x1, x2, y1, y2 As Double Console.Write("Enter coordinates for point A: ") x1 = Console.ReadLine() y1 = Console.ReadLine() Console.Write("Enter coordinates for point B: ") x2 = Console.ReadLine() y2 = Console.ReadLine() d = Math.Sqrt((x1 - x2) ^ 2 + (y1 - y2) ^ 2) Console.Write("Distance between points: " & d) Console.ReadKey() End Sub
Python
import math print("Enter coordinates for point A: ") x1, y1 = float(input()), float(input()) print("Enter coordinates for point B: ") x2, y2 = float(input()), float(input()) d = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) print("Distance between points:", d)