How to calculate the Euclidean distance between two points in C

1 Answer

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

// The Euclidean distance is a measure of the straight-line distance 
// between two points in a 2D or 3D space

// Function to calculate Euclidean distance
double calculateEuclideanDistance(double x1, double y1, double x2, double y2) {
    return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}

int main() {
    double x1 = 3.0, y1 = 4.0;
    double x2 = 5.0, y2 = 9.0;

    double distance = calculateEuclideanDistance(x1, y1, x2, y2);

    printf("Euclidean Distance: %.5f\n", distance);

    return 0;
}



/*
run:
   
Euclidean Distance: 5.38516
   
*/

 



answered Oct 11, 2025 by avibootz
...