Software & Finance





Visual C++ - Distance between the two points





Here is the Visual C++ program for the Distance between the two points.

 

It uses the following formula give points are (x1,y1) and (x2, y2)

 

SQRT( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) )

 


Source Code


#include <iostream>

#include <math.h>

 

void main()

{

    float distance;

    int x1, y1, x2, y2;

    int dx, dy;

 

    std::cout << "Program for distance between the two points\n";

 

    std::cout << "Enter X1: ";

    std::cin >> x1;

 

    std::cout << "Enter Y1: ";

    std::cin >> y1;

 

    std::cout << "Enter X2: ";

    std::cin >> x2;

 

    std::cout << "Enter Y2: ";

    std::cin >> y2;

 

    dx = x2 - x1;

    dy = y2 - y1;

 

    distance = sqrt((double) dx*dx + dy*dy);

    std::cout << "Distance between (" << x1 << ", " << y1 << " and (" << x2 << ", " << y2 << ") = ";

    std::cout << "SQRT(" << dx*dx + dy*dy << ") = " << distance;

}

Output


 

Program for distance between the two points

Enter X1: 10

Enter Y1: 10

Enter X2: 30

Enter Y2: 30

Distance between (10, 10) and (30, 30) = SQRT(800) = 28.2843