Program to calculate distance between two points - GeeksforGeeks

04 May, 2022
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) isDistance = We can get above formula by simply applying Pythagoras theorem 
 
Below is the implementation of above idea.
 
C++
C
Java
Python3
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std; // Function to calculate distancefloat distance(int x1, int y1, int x2, int y2){    // Calculating distance    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);} // Drivers Codeint main(){    cout << distance(3, 4, 4, 3);    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
#include <math.h>#include <stdio.h> // Function to calculate distancefloat distance(int x1, int y1, int x2, int y2){    // Calculating distance    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);} // Drivers Codeint main(){    printf("%f", distance(3, 4, 4, 3));    return 0;} // This code is contributed by Aditya Kumar (adityakumar129)
// 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 Aditya Kumar (adityakumar129)
# Python3 program to calculate# distance between two points import math # Function to calculate distancedef distance(x1 , y1 , x2 , y2):     # Calculating distance    return math.sqrt(math.pow(x2 - x1, 2) +                math.pow(y2 - y1, 2) * 1.0) # Drivers Codeprint("%.6f"%distance(3, 4, 4, 3)) # This code is contributed by "Sharad_Bhardwaj".
// C# code to compute distanceusing 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.
<?php// PHP code to compute distance // Function to calculate distancefunction distance($x1, $y1, $x2, $y2){         // Calculating distance    return sqrt(pow($x2 - $x1, 2) +                pow($y2 - $y1, 2) * 1.0);} // Driver Codeecho(distance(3, 4, 4, 3)); // This code is contributed by Ajit.?>
<script> // Function to calculate distancefunction distance(x1, y1, x2,  y2){    // Calculating distance    return Math.sqrt(Math.pow(x2 - x1, 2) +                Math.pow(y2 - y1, 2) * 1.0);} // Drivers Codedocument.write(distance(3, 4, 4, 3)); // This code is contributed by noob2000.</script>
Output:  
1.41421
 
jit_t
noob2000
adityakumar129
Mathematical
Misc
School Programming
Misc
Mathematical
Misc
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Print all possible combinations of r elements in a given array of size n
Top 10 algorithms in Interview Questions
vector::push_back() and vector::pop_back() in C++ STL
Overview of Data Structures | Set 1 (Linear Data Structures)
How to write Regular Expressions?
Minimax Algorithm in Game Theory | Set 3 (Tic-Tac-Toe AI - Finding optimal move)