Program to calculate distance between two points Last Updated : 10 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice You are given two coordinates (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 : 5Input : x1, y1 = (3, 4) x2, y2 = (4, 3)Output : 1.41421Calculate the distance between two points.We will use the distance formula derived from Pythagorean theorem. The formula for distance between two point (x1, y1) and (x2, y2) isDistance = \sqrt{(x2-x1)^{2} + (y2-y1)^{2}}We can get above formula by simply applying Pythagoras theoremcalculate distance between two pointsBelow 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) { return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0); } // Drivers Code int main() { cout << distance(3, 4, 4, 3); return 0; } C #include <math.h> #include <stdio.h> // Function to calculate distance float distance(int x1, int y1, int x2, int y2) { return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0); } // Drivers Code int main() { printf("%f", distance(3, 4, 4, 3)); return 0; } Java // Java code to compute distance class GFG { // Function to calculate distance static double distance(int x1, int y1, int x2, int y2) { 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); } } Python # Python3 program to calculate # distance between two points import math # Function to calculate distance def distance(x1 , y1 , x2 , y2): return math.sqrt(math.pow(x2 - x1, 2) + math.pow(y2 - y1, 2)) # Drivers Code print("%.6f"%distance(3, 4, 4, 3)) 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) { 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); } } JavaScript // Function to calculate distance function distance(x1, y1, x2, y2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)); } // Driver Code console.log(distance(3, 4, 4, 3).toFixed(6)); Output1.41421 Comment More infoAdvertise with us Next Article Program to calculate distance between two points S ShivamKD Follow Improve Article Tags : Misc Mathematical DSA Basic Coding Problems Practice Tags : MathematicalMisc Similar Reads Program to calculate distance between two points in 3 D Given two coordinates (x1, y1, z1) and (x2, y2, z2) in 3 dimension. The task is to find the distance between them.Examples : Input: x1, y1, z1 = (2, -5, 7) x2, y2, z1 = (3, 4, 5) Output: 9.2736184955 Input: x1, y1, z1 = (0, 0, 0) x2, y2, z1 = (1, 1, 1) Output: 1.73205080757 Approach: The formula for 5 min read How to Calculate the Distance Between Two Points? Answer: To calculate the distance between Two Points, Distance Formula is used, which is d = \sqrt{[(x_2 - x_1 )^2 +(y_2 - y_1)^2]}The length of the line segment connecting two points is defined as the distance between them. The length of the line segment connecting the specified coordinates can be 6 min read Program to calculate the area between two Concentric Circles Given two concentric circles with radius X and Y where (X > Y). Find the area between them. You are required to find the area of the green region as shown in the following image: Examples: Input : X = 2, Y = 1 Output : 9.42478 Input : X = 4, Y = 2 Output : 37.6991 Approach:The area between the tw 4 min read Program for distance between two points on earth Given latitude and longitude in degrees find the distance between two points on the earth. Image Source : WikipediaExamples: Input : Latitude 1: 53.32055555555556 Latitude 2: 53.31861111111111 Longitude 1: -1.7297222222222221 Longitude 2: -1.6997222222222223 Output: Distance is: 2.0043678382716137 K 7 min read Distance between two points travelled by a boat Write a program to determine the distance(D) between two points traveled by a boat, given the speed of boat in still water(B), the speed of the stream(S), the time taken to row a place and come back i.e T. Examples: Input : B = 5, S = 1, T = 1 Output : D = 2.4 Input : B = 5, S = 2, T = 1 Output : D 4 min read Shortest distance between a point and a circle Given a circle with a given radius has its centre at a particular position in the coordinate plane. In the coordinate plane, another point is given. The task is to find the shortest distance between the point and the circle.Examples: Input: x1 = 4, y1 = 6, x2 = 35, y2 = 42, r = 5 Output: 42.5079 Inp 4 min read Distance between two parallel lines Given are two parallel straight lines with slope m, and different y-intercepts b1 & b2.The task is to find the distance between these two parallel lines.Examples: Input: m = 2, b1 = 4, b2 = 3 Output: 0.333333 Input: m = -4, b1 = 11, b2 = 23 Output: 0.8 Approach: Let PQ and RS be the parallel lin 4 min read Calculate the Manhattan Distance between two cells of given 2D array Given a 2D array of size M * N and two points in the form (X1, Y1) and (X2 , Y2) where X1 and X2 represents the rows and Y1 and Y2 represents the column. The task is to calculate the Manhattan distance between the given points. Examples: Input: M = 5, N = 5, X1 = 1, Y1 = 2, X2 = 3, Y2 = 3Output: 3Ex 4 min read Minimum Distance between Two Points You are given an array arr[] of n distinct points in a 2D plane, where each point is represented by its (x, y) coordinates. Find the minimum Euclidean distance between two distinct points.Note: For two points A(px,qx) and B(py,qy) the distance Euclidean between them is:Distance = \sqrt{(p_{x}-q_{x}) 15+ min read Distance between two parallel Planes in 3-D You are given two planes P1: a1 * x + b1 * y + c1 * z + d1 = 0 and P2: a2 * x + b2 * y + c2 * z + d2 = 0. The task is to write a program to find distance between these two Planes. Examples : Input: a1 = 1, b1 = 2, c1 = -1, d1 = 1, a2 = 3, b2 = 6, c2 = -3, d2 = -4 Output: Distance is 0.952579344416 I 8 min read Like