C Program To Calculate Distance Between Two Points

This article is a tutorial which explains how to write a C program to calculate or find the distance between two points.
Formula to Calculate Distance Between Two Points:

distance = √(a2 + b2)

i.e. the distance between two points can be 
found out by calculating the square root of
sum of squares of a and b,

Where a = X2 -X1 and
      b = Y2 - Y1.

(X1, X2) represent first point and

(Y1, Y2) represent second point.

C Program to Calculate the Distance Between Two Points

/* Aim: Write a C program to calculate Distance Between Two Points */

#include<stdio.h>
#include<math.h>

int main()
{
    int x1,x2,y1,y2,a,b;
    float distance;
 
    printf("\n Enter co-ordinates of first point (x1,x2): ");
    scanf("%d%d",&x1,&x2);

    printf("\n Enter co-ordinates of second point (y1,y2): ");
    scanf("%d%d",&y1,&y2);
 
    a=x2-x1;
    b=y2-y1;
    distance = sqrt((a*a)+(b*b));

    printf("\n The Distance Between Two Points is %f \n \n", distance);

    return 0;
}

Output:

 Enter co-ordinates of first point (x1,x2): 4 6

 Enter co-ordinates of second point (y1,y2): 8 -9

 The Distance Between Two Points is 17.117243

Explanation:

Accept the co-ordinate values of both points. Calculate a and b according to the formula. Add a2 and b2 then take the square root of result of this sum to calculate the distance between given two points. Display the distance.