Write a program that inputs the x, y coordinates for two points and computes distance between two points using the formula: Distance = sqrt (x2 – x1)2 + (y2 – y1)2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <cmath> using namespace std; int main() { double x1, y1, x2, y2, distance; cout << "Enter x coordinate of first point (x1): "; cin >> x1; cout << "Enter y coordinate of first point (y1): "; cin >> y1; cout << "Enter x coordinate of second point (x2): "; cin >> x2; cout << "Enter y coordinate of second point (y2): "; cin >> y2; // Calculating distance between two points distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); cout << "Distance between the two points: " << distance << endl; return 0; } |
Output
Enter x coordinate of first point (x1): 4
Enter y coordinate of first point (y1): 6
Enter x coordinate of second point (x2): 7
Enter y coordinate of second point (y2): 4
Distance between the two points: 3.60555