Description: This problem involves calculating the Euclidean distance between two points in a 2D plane. You will be given two points, (x1, y1) and (x2, y2) that are taken as an input, and need to calculate the distance between them using the formula:
function distance(x1, y1, x2, y2)
dx ← x2 - x1
dy ← y2 - y1
dist ← sqrt(dx^2 + dy^2)
return dist
end function
// Example usage:
x1 ← 3
y1 ← 4
x2 ← 7
y2 ← 1
result ← distance(x1, y1, x2, y2)
print result // Output: 5
import math
def distance(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
dist = math.sqrt(dx**2 + dy**2)
return dist
# Example usage
x1, y1 = 3, 4
x2, y2 = 7, 1
result = distance(x1, y1, x2, y2)
print(result) # Output: 5.0
public class Main {
public static double distance(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
public static void main(String[] args) {
int x1 = 3, y1 = 4;
int x2 = 7, y2 = 1;
double result = distance(x1, y1, x2, y2);
System.out.println(result); // Output: 5.0
}
}
#include <iostream>
#include <cmath>
using namespace std;
double distance(int x1, int y1, int x2, int y2) {
int dx = x2 - x1;
int dy = y2 - y1;
return sqrt(dx * dx + dy * dy);
}
int main() {
int x1 = 3, y1 = 4;
int x2 = 7, y2 = 1;
double result = distance(x1, y1, x2, y2);
cout << result << endl; // Output: 5
return 0;
}