# Program to calculate distance between two points

You are given two co-ordinates (x1, y1) and (x2, y2) of a two dimensional graph. Find the distance between them.

Examples:

Input : x1, y1 = (3, 4) x2, y2 = (7, 7) Output : 5 Input : x1, y1 = (3, 4) x2, y2 = (4, 3) Output : 1.41421

We will use the distance formula derived from Pythagorean theorem. The formula for distance between two point (x1, y1) and (x2, y2) is

Distance =

We can get above formula by simply applying Pythagoras theorem

Below is the implementation of above idea.

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate distance ` `float` `distance(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `// Calculating distance ` ` ` `return` `sqrt` `(` `pow` `(x2 - x1, 2) + ` ` ` `pow` `(y2 - y1, 2) * 1.0); ` `} ` ` ` `// Drivers Code ` `int` `main() ` `{ ` ` ` `cout << distance(3, 4, 4, 3); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to compute distance ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calculate distance ` `static` `double` `distance(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` `{ ` ` ` `// Calculating distance ` ` ` `return` `Math.sqrt(Math.pow(x2 - x1, ` `2` `) + ` ` ` `Math.pow(y2 - y1, ` `2` `) * ` `1.0` `); ` `} ` ` ` `//Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `System.out.println(Math.round(distance(` `3` `, ` `4` `, ` `4` `, ` `3` `)*` `100000.0` `)/` `100000.0` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to calculate ` `# distance between two points ` ` ` `import` `math ` ` ` `# Function to calculate distance ` `def` `distance(x1 , y1 , x2 , y2): ` ` ` ` ` `# Calculating distance ` ` ` `return` `math.sqrt(math.` `pow` `(x2 ` `-` `x1, ` `2` `) ` `+` ` ` `math.` `pow` `(y2 ` `-` `y1, ` `2` `) ` `*` `1.0` `) ` ` ` `# Drivers Code ` `print` `(` `"%.6f"` `%` `distance(` `3` `, ` `4` `, ` `4` `, ` `3` `)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# code to compute distance ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calculate distance ` ` ` `static` `double` `distance(` `int` `x1, ` `int` `y1, ` `int` `x2, ` `int` `y2) ` ` ` `{ ` ` ` `// Calculating distance ` ` ` `return` `Math.Sqrt(Math.Pow(x2 - x1, 2) + ` ` ` `Math.Pow(y2 - y1, 2) * 1.0); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `Console.WriteLine(Math.Round(distance(3, 4, 4, 3) ` ` ` `* 100000.0)/100000.0); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to compute distance ` ` ` `// Function to calculate distance ` `function` `distance(` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `) ` `{ ` ` ` ` ` `// Calculating distance ` ` ` `return` `sqrt(pow(` `$x2` `- ` `$x1` `, 2) + ` ` ` `pow(` `$y2` `- ` `$y1` `, 2) * 1.0); ` `} ` ` ` `// Driver Code ` `echo` `(distance(3, 4, 4, 3)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

1.41421

## Recommended Posts:

- Program to calculate distance between two points in 3 D
- Program for distance between two points on earth
- Distance between two points travelled by a boat
- Hammered distance between N points in a 2-D plane
- Calculate speed, distance and time
- Check whether it is possible to join two points given on circle such that distance between them is k
- Find points at a given distance on a line of given slope
- Find the maximum possible distance from origin using given points
- Haversine formula to find distance between two points on a sphere
- C program to calculate the value of nPr
- Program to calculate value of nCr
- Program to calculate age
- Program to calculate the value of nCr Efficiently
- Write a program to calculate pow(x,n)
- Efficient program to calculate e^x

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.