Skip to content
Home » C program to find the distance between two given points

C program to find the distance between two given points

Learn about C program to find the distance between two given points in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C program to find the distance between two given points

The Pythagorean theorem is used to obtain the distance formula. To calculate the distance between two points (x1, y1) and (x2, y2), simply use the coordinates of these ordered pairs and the formula.

Program:

#include<stdio.h>
#include<math.h>
int main()
{
   int x1, y1, x2, y2, x, y, distance;

   // input first point's coordinates
   printf("Enter coordinates of first point: ");
   scanf("%d %d",&x1, &y1);

   // input second point's coordinates
   printf("Enter coordinates of second point: ");
   scanf("%d %d",&x2, &y2);

   x = (x2-x1);
   y = (y2-y1);

   distance = sqrt(x*x + y*y);

   // print result
   printf("Distance = %d", distance);

   return 0;
}

Output:

Enter coordinates of first point: 0 0
Enter coordinates of second point: 8 6
Distance = 10

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to Calculate Compound Interest
C Program to Calculate Simple Interest