In this article, we will write a C program to find the distance between two points.

The program asks the user to enter the XY coordinates of both points and returns the distance between them as a result.

**Sample Input:**

```
Enter the XY coordinates of first point: 1 1
Enter the XY coordinates of second point: 3 4
```

**Output:**

`The distance between the two points is: 3.605551`

Before writing the program, let’s first see what is the formula to find the distance between two points.

As per the Pythagoras theorem, If we have two points A and B with their XY coordinates as (x1, y1) and (x2, y2), then the distance between them is given by:

To implement the above formula in our C program, we can follow the below mentioned steps:

- Declare four integer variables
`x1`

,`y1`

,`x2`

,`y2`

to get the XY coordinates of both points. As the distance between the two points can be an integer or a decimal point value, therefore, we have to keep the`distance`

variable as a float data type. - Take the inputs(XY coordinates) from the user using the scanf() function and store them in the respective variables.
- Find the difference between the XX and YY coordinates of both points i.e. (x2 – x1) & (y2 -y1) and get their squares i.e. (x2 – x1)*(x2 – x1) & (y2 – y1)*(y2 – y1).
- Get the sum of the squares and use the sqrt() function of the
`math.h`

header library to get their square root. - Store the result in the
`distance`

variable and print it on the screen using the printf() function.

See the implementation of the above steps in the following C program:

// C program to find the distance between two points #include <stdio.h> #include <math.h> int main(){ // Declare the variables int x1, y1, x2, y2; float distance; printf("Enter the XY coordinates of first point: "); scanf("%d %d", &x1, &y1); printf("Enter the XY coordinates of second point: "); scanf("%d %d", &x2, &y2); // Calculate the distance distance = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); // Print the result printf("The distance between the two points is: %f", distance); return 0; }

**Output:**

Enter the XY coordinates of first point: 1 1 Enter the XY coordinates of second point: 3 4 The distance between the two points is: 3.605551

## Method 2: Using the pow() Function

In the previous approach, to get the squares of (x2 – x1) and (y2 – y1), we multiplied them by their self which somewhat makes the code lengthy.

We can make our code a bit cleaner by using the `pow()`

function of the `math.h`

library instead of multiplying them by their self.

The pow() function takes two arguments, the base value and the power value and returns the result of raising the base value to the power of the second argument. In other words, it calculates the exponentiation of the base value.

The syntax of the pow() function is as follows:

double pow(double base, double power);

To use the pow() function in our C program we have to include the `math.h`

header file at the top of our code.

See the modified version of the previous program with the pow() function:

// C program to find the distance between two points #include <stdio.h> #include <math.h> int main(){ // Declare the variables int x1, y1, x2, y2; float distance; printf("Enter the XY coordinates of first point: "); scanf("%d %d", &x1, &y1); printf("Enter the XY coordinates of second point: "); scanf("%d %d", &x2, &y2); // Calculate the distance distance = sqrt (pow((x2-x1), 2) + pow((y2-y1), 2)); // Print the result printf("The distance between the two points is: %f", distance); return 0; }

**Output:**

Enter the XY coordinates of first point: 1 1 Enter the XY coordinates of second point: 3 4 The distance between the two points is: 3.605551

Thanks for reading.