Calculate Distance Between Two Points C Program

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


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.

Tushar Soni

I am Tushar Soni, Co - Founder of CodingAlpha. I am a computer science student from India and passionate about Web Development and Programming. Connect with me on Facebook | LinkedIn | Google Plus

3 thoughts on “Calculate Distance Between Two Points C Program

  • June 2, 2016 at 8:36 pm

    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.

    • July 31, 2016 at 8:04 pm

      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.

  • August 21, 2016 at 1:45 am

    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.


Join The Discussion