Calculate Distance Between Two Points C Program

By | May 23, 2016

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: z2 = x2 + y2

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

 

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

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

Output

C Program To Calculate Distance Between Two Points using Function, Pointers, Structures

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.

5 thoughts on “Calculate Distance Between Two Points C Program

  1. Shubham Soni

    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.

    Reply
    1. Tushar Soni Post author

      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.

      Reply
  2. Parag Vidhate

    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.

    Reply
  3. Anonymous

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

    Reply

Let's Discuss