Welcome to our website! We are adding new data every day, so please check back often for updates.
Posts

How to Write a C Program to Calculate the Distance Between Two Points

write a C program that can calculate the distance between any two points in a two-dimensional plane using the Pythagorean theorem.

If you are learning C programming, you might have encountered some problems that require you to calculate the distance between two points. For example, you might want to find the length of a line segment, the perimeter of a triangle, or the area of a circle. In this program, I will show you how to write a C program that can calculate the distance between any two points in a two-dimensional plane.

What is the Distance Between Two Points?

The distance between two points is the shortest length of a straight line that connects them. In geometry, this is also known as the Euclidean distance or the Pythagorean distance. To find the distance between two points, we can use the Pythagorean theorem, which states that the square of the hypotenuse of a right triangle is equal to the sum of the squares of the other two sides. In other words, if we have a right triangle with sides a, b, and c, where c is the hypotenuse, then we have: c^2 = a^2 + b^2

Now, suppose we have two points A and B with coordinates (x1, y1) and (x2, y2) respectively. We can form a right triangle by drawing a horizontal line from A to C and a vertical line from C to B, where C has coordinates (x2, y1). Then, we can apply the Pythagorean theorem to find the distance between A and B: d^2 = AC^2 + CB^2

where d is the distance between A and B, AC is the length of the horizontal side, and CB is the length of the vertical side. To find AC and CB, we can use the difference between the x-coordinates and y-coordinates of the points:

AC = x2 - x1

CB = y2 - y1

Therefore, d^2 = (x2 - x1)^2 + (y2 - y1)^2

To get d, we just need to take the square root of both sides: d = sqrt((x2 - x1)^2 + (y2 - y1)^2 This is the formula that we will use to calculate the distance between two points in our C program.

C Program

To write a C program that can calculate the distance between two points, we need to do three things:

  • Declare a function that takes four parameters (the coordinates of the two points) and returns a double value (the distance).
  • Define the function using the formula that we derived above.
  • Write a main function that prompts the user to enter the coordinates of the two points and calls the distance function to get and print the result.

Here is how our program looks like:

#include <stdio.h>

#include <math.h>

// A function to calculate the distance between two points

double distance(double x1, double y1, double x2, double y2) {

  // Use the Pythagorean theorem to find the distance

  double dx = x2 - x1; // The difference in x-coordinates

  double dy = y2 - y1; // The difference in y-coordinates

  double d = sqrt(dx * dx + dy * dy); // The distance

  return d; // Return the distance

}

// A main function to test the distance function

int main() {

  // Declare some variables to store the coordinates of two points

  double x1, y1, x2, y2;

  // Prompt the user to enter the coordinates of the first point

  printf("Enter the coordinates of the first point (x1, y1): ");

  scanf("%lf %lf", &x1, &y1);

  // Prompt the user to enter the coordinates of the second point

  printf("Enter the coordinates of the second point (x2, y2): ");

  scanf("%lf %lf", &x2, &y2);

  // Call the distance function and store the result in a variable

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

  // Print the result to the standard output

  printf("The distance between (%.2f, %.2f) and (%.2f, %.2f) is %.2f\n", x1, y1, x2, y2, d);

  // Return 0 to indicate successful termination

  return 0;

}

Output

Enter the coordinates of the first point (x1, y1): 3 4 
Enter the coordinates of the second point (x2, y2): -1 2
The distance between (3.00, 4.00) and (-1.00, 2.00) is 4.47

Conclusion

In this program, I have shown you how to write a C program that can calculate the distance between two points using the Pythagorean theorem. I hope you have learned something useful and enjoyed reading this post. If you have any questions or feedback, please leave a comment below. Thank you for reading!



We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!

Post a Comment

For more Tech content follow us on Social media
© Ruturaj Khansole. All rights reserved. Distributed by ASThemesWorld