calculate distance between 2 points X Y axis

Calculating Distance Between Two Points in C

To calculate the distance between two points on the X-Y axis, you can use the distance formula, which is derived from the Pythagorean theorem. The formula is:

Distance = √((x2 - x1)^2 + (y2 - y1)^2)

Where (x1, y1) and (x2, y2) are the coordinates of the two points.

Here's a C program to calculate the distance between two points:

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

int main() {
    float x1, y1, x2, y2, distance;

    printf("Enter the coordinates of point 1 (x1 y1): ");
    scanf("%f %f", &x1, &y1);

    printf("Enter the coordinates of point 2 (x2 y2): ");
    scanf("%f %f", &x2, &y2);

    distance = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));

    printf("Distance between the two points: %f\n", distance);

    return 0;
}

In this program: 1. We prompt the user to enter the coordinates of the two points. 2. We then calculate the distance using the distance formula. 3. Finally, we display the calculated distance.

This program uses the sqrt function from the math.h library to calculate the square root and the pow function to calculate the power of a number.

[[SOURCE 1]]