C Program to Find Distance Between Two Points

In this tutorial, i am going to show you how to find distance between two points in c program.

Algorithm to Find Distance Between Two Points

Follow the below given algorithm to write a program to find the distance between two points; as follows:

  • Step 1: Start program.
  • Step 2: Read two points numbers from user and store them into variables.
  • Step 3: Find distance between two points using this formula (sqrt( (x2 – x1)*(x2 – x1) + (y2 – y1)*(y2 – y1) )) and store result in variable.
  • Step 4: Print distance between two points.
  • Step 5: End program.

C Program to Find Distance Between Two Points

int main()  
    float x1, y1, x2, y2, distance;  
    printf("Enter point 1 (x1, y1)\n");  
    scanf("%f%f", &x1, &y1);  
    printf("Enter point 2 (x2, y2)\n");  
    scanf("%f%f", &x2, &y2);  
    distance = sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) );  
    printf("Distance between (%0.2f, %0.2f) and (%0.2f, %0.2f) is %0.2f\n", x1, y1, x2, y2, distance);  
    return 0;  

The result of the above c program; as follows:

Enter point 1 (x1, y1) :- 10 20
Enter point 2 (x2, y2) :- 25 30
Distance between (10.00, 20.00) and (25.00, 30.00) is 18.03

More C Programming Tutorials

Leave a Comment