15. Program that inputs x, y coordinates for two points and computes the distance between two points.
Chapter 4:
Input And Output
Programming Exercise
Problem # 15:
Write a program that inputs x, y coordinates for two points and computes the
distance between two points using the formula: Distance = sqrt( (x2-x1)^2 + (y2-y1)^2 )
Solution:
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
int x1, x2, y1, y2; //x-coordinate, y-coordinate
float d; //distance
cout<<"Enter x, y coordinate for point 1 : ";
cin>>x1>>y1;
cout<<"Enter x, y coordinate for point 2 : ";
cin>>x2>>y2;
d = sqrt( pow(x2-x1,2) + pow(y2-y1,2) );
cout<<"Distance is: "<<d;
getch();
return 0;
}
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
int x1, x2, y1, y2; //x-coordinate, y-coordinate
float d; //distance
cout<<"Enter x, y coordinate for point 1 : ";
cin>>x1>>y1;
cout<<"Enter x, y coordinate for point 2 : ";
cin>>x2>>y2;
d = sqrt( pow(x2-x1,2) + pow(y2-y1,2) );
cout<<"Distance is: "<<d;
getch();
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
14. Program to calculate the volume (V) of a cube by taking meaures from the user.
Comments
Post a Comment