Calculating Distance Between the Two points
Q. Write a C program to calculate the distance between two points.
Ans:
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, distance;
printf("Enter point 1 (x, y): ");
scanf("%f %f", &x1, &y1);
printf("Enter point 2 (x, y): ");
scanf("%f %f", &x2, &y2);
// Calculate distance using the formula
distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("The distance between (%.2f, %.2f) and (%.2f, %.2f) is %.2f\n", x1, y1, x2, y2, distance);
return 0;
}

Comments
Post a Comment