Saturday 31 January 2015

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

Structure of the Problem Requirements


 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 it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment