## C Program To Calculate Distance Between Two Points

Here’s a Code to Calculate Distance between Two Points in C Programming Language. This C Code to Find Distance between Two Points on a Plane is a very simple one and only requires calculation using a Formula.

Every Point on a Graph has Two Coordinates. In this C Program for Calculation of Distance of Two Points, we make use of the same formula. Let us assume that the Two Points are M and N. Therefore, M and N will have Two Coordinates each both representing M – Axis Coordinate and N – Axis Coordinate.

Therefore,** M = ( X1, Y1)** and **N = (X2, Y2)**

The Distance Formula between Two Points is derived from the Pythagoras Theorem. According to the Pythagoras Theorem,

**(Hypotenuse) ^{2} = (Base)^{2} + (Height)^{2}**

**Example: z ^{2} = x^{2} + y^{2}**

#### Formula To Calculate Distance Between Two Points in C Programming

**Distance Between Two Points = √ (x1 – y1) ^{2} + (x2 – y2)^{2}**

**Also Read: Calculate Roots of a Quadratic Equation in C Programming**

**Command: gcc test.c -lm**

#### Method 1: C Program To Find Distance Between Two Points using Function

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> #include <stdlib.h> #include <math.h> float distance_calculator(float x1, float x2, float y1, float y2) { float distance; distance = sqrt((x1 - y1) * (x1 - y1) + (x2 - y2) * (x2 - y2)); return distance; } int main() { float result, a, b, c, d; printf("\nEnter The Coordinates of Point A:\n"); printf("\nX - Axis Coordinate: \t"); scanf("%f", &a); printf("\nY - Axis Coordinate: \t"); scanf("%f", &b); printf("\nEnter The Coordinates of Point B:\n"); printf("\nY - Axis Coordinate:\t"); scanf("%f", &c); printf("\nY - Axis Coordinate: \t"); scanf("%f", &d); result = distance_calculator(a, b, c, d); printf("\nDistance between Points A and B: %f\n", result); return 0; } |

#### Method 2: C Program To Calculate Distance Between Two Co-ordinates using Pointers

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <stdio.h> #include <stdlib.h> #include <math.h> float distance_finder(float *x1, float *x2, float *y1, float *y2) { float distance; distance = sqrt((*x1 - *y1) * (*x1 - *y1) + (*x2 - *y2) * (*x2 - *y2)); return distance; } int main() { float result, a, b, c, d; printf("\nEnter The Coordinates of Point A:\n"); printf("\nX - Axis Coordinate: \t"); scanf("%f", &a); printf("\nY - Axis Coordinate: \t"); scanf("%f", &b); printf("\nEnter The Coordinates of Point B:\n"); printf("\nY - Axis Coordinate:\t"); scanf("%f", &c); printf("\nY - Axis Coordinate: \t"); scanf("%f", &d); result = distance_finder(&a, &b, &c, &d); printf("\nDistance between Points A and B: %f\n", result); return 0; } |

#### Method 3: C Program To Find Distance Between Two Points using Structures

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | struct Points { float x, y; }; float distance_calculator(struct Points m, struct Points n) { float result; result = sqrt((m.x - n.x) * (m.x - n.x) + (m.y - n.y) *(m.y - n.y)); return result; } int main() { struct Points m, n; printf("\nEnter The Coordinates of Point A:\n"); printf("\nX - Axis Coordinate: \t"); scanf("%f", &m.x); printf("\nY - Axis Coordinate: \t"); scanf("%f", &m.y); printf("\nEnter The Coordinates of Point B:\n"); printf("\nY - Axis Coordinate:\t"); scanf("%f", &n.x); printf("\nY - Axis Coordinate: \t"); scanf("%f", &n.y); printf("\nDistance between Points A and B: %f\n", distance_calculator(m, n)); return 0; } |

#### Output

If you find any doubts or compilation errors in this C Program To Calculate Distance Between Two Points, let us know about it in the comment section.

I see that this Distance Calculation between Two Points is compiled in Ubuntu. Do we have to use the -lm parameter if we compile this program in Windows. I am using Codeblocks Editor.

I don’t think so CodeBlocks will require using the command lm. You just need to hit the compile and run button in the menu header above.

Thanks for all these 3 methods of finding the distance between two planar points in C Programming Language. I believe the Structures method is better than the above two methods as it represents points and its co – ordinates in a better and easy to understand manner.

believe that the formula for the first method should be dist= sqrt((x2-x1)*x2-x1)+(y2-y1)*(y2-y1))