Online Compiler C

#include <stdio.h> #include <math.h> int main() { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); int dx = x1 - x2; int dy = y1 - y2; double distance = sqrt(dx * dx + dy * dy); printf("%.2f\n", distance); return 0; }
This code reads four integers representing two points (x1,y1) and (x2,y2), calculates the Euclidean distance between them, and prints the result with two decimal places.

- The formula for distance uses the difference in x and y coordinates. Check whether subtracting x2 from x1 or x1 from x2 changes the result. The squared difference is the same either way, so the order does not matter for the final distance.
- The variable `distance` is declared as `double`, but `dx` and `dy` are `int`. When you compute `dx * dx + dy * dy`, the result is an integer. Passing an integer to `sqrt` is fine, but be aware that if the sum is very large, it might overflow an `int`. Consider using `double` for the differences or casting to avoid potential overflow.