How to Calculate Distance Between two points in C++ program

The C++ math library is used to perform some mathematical operations in the program and. For calculation of distance we used Euclidean formula which is the most common and easy formula to find the distance. Complete formula of distance calculation is AB = √ ( x2 - x1 )² + (Y2 - Y1)².

Source Code

#include<iostream>
#include<math.h>
using namespace std;
int main ()
{
 cout<<"\t \t \t LEP Tutorials \n \n";
 float distance;
 int x1,y1,x2,y2;
 int dx,dy;
 cout<<" Enter the X1 Value  : ";
 cin>>x1;
 cout<<" Enter the Y1 Value  : ";
 cin>>y1;
 cout<<" Enter the X2 Value  : ";
 cin>>x2;
 cout<<" Enter the Y2 Value  : ";
 cin>>y2;
 dx = x2 - x1;
 dy = y2 - y1;
 distance = sqrt((double) dx*dx + dy*dy);
 cout<<endl;
 cout<<" Points"<<"("<<x1<<","<<y1<<")"<<"("<<x2<<","<<y2<<")"<<endl<<endl;
 cout<<" Distance  : "<<distance<<endl;
}

Output of the Program

How to Calculate Distance Between two points in C++ program

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment