C++ Function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double.

//C++ Function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double.



#include <iostream>
#include <cmath>
using namespace std;
float findDistance(int,int,int,int);
int main() 
{
	int x1,x2,y1,y2;
  	float r=0;
  	cout << "Two Points (x1,x2) & (y1,y2) : \n";

  	cout << "Enter x1:\n";
  	cin >> x1;
  
  	cout << "Enter x2:\n";
  	cin >> x2;
  
 	cout << "Enter y1:\n";
  	cin >> y1;
  
  	cout << "Enter y2:\n";
  	cin >> y2;
	  
  	r=findDistance(x1,y1,x2,y2);
  	cout << "Distance between the 2 points is " << r ;
        return 0;
} 
float findDistance(int x1, int y1, int x2, int y2)
{
  	float d;
  	d=sqrt((pow(x2-x1,2))+(pow(y2-y1,2)));
  	return d;
}

Output :
Two Points (x1,x2) & (y1,y2) :

Enter x1:
1
Enter x2:
2
Enter y1:
3
Enter y2:
4
Distance between the 2 points is 1.414214



Leave a comment