# C Program to Calculate Distance Between Two Points

In this post, we will learn how to calculate the distance between two points using the C Programming language.

Distance between two points is the length of the line segment that connects the two given points. The formula for the distance d, between two points whose coordinates are (x1, y1) and (x2, y2) is:

D = [(x2 – x1) – (y2 – y1)]½

We will be using the above formula in our program to calculate the distance between two points.

So, without further ado, let’s begin this tutorial.

Contents

## C Program to Calculate Distance Between Two Points

```// C Program to Calculate Distance Between Two Points
#include <stdio.h>
#include <math.h>
int main(){
int num1, num2, x1, x2, y1, y2;
float distance;

printf("Enter the X and Y Coordinates of First Point: ");
scanf("%d %d", &x1, &y1);
printf("Enter the X and Y Coordinates of Second Point: ");
scanf("%d %d", &x2, &y2);

// Calculating the distance
num1 = x2 - x1;
num2 = y2 - y1;
distance = sqrt((num1 * num1) + (num2 * num2));

// Displaying output
printf("Distance: %.2f", distance);
return 0;
}
```

Output

``````Enter the X and Y Coordinates of First Point: 4 7
Enter the X and Y Coordinates of Second Point: 2 12
Distance: 5.39
``````

## How Does This Program Work ?

```    int num1, num2, x1, x2, y1, y2;
float distance;
```

In this program, we have declared some int and float data type variables which will store the coordinates of the points.

```    // Asking for input
printf("Enter the X and Y Coordinates of First Point: ");
scanf("%d %d", &x1, &y1);
printf("Enter the X and Y Coordinates of Second Point: ");
scanf("%d %d", &x2, &y2);
```

Then, the user is asked to enter the value of the coordinates.

```    // Calculating the distance
num1 = x2 - x1;
num2 = y2 - y1;
distance = sqrt((num1 * num1) + (num2 * num2));
```

Now, we calculate the distance between two points using the formula:

D = [(x2 – x1) – (y2 – y1)]½

```    // Displaying output
printf("Distance: %.2f", distance);
```

Finally, the result is printed using the printf() function.

## Conclusion

I hope after going through this post, you understand how to calculate the distance between two points using C Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.