Home Uncategorized C program to calculate distance between the two points.

C program to calculate distance between the two points.

SHARE


Write a C program to calculate the distance between the two points.

Solution:

#include<stdio.h>

#include<math.h>

int main()

{

   int x1,x2,y1,y2;

   int x,y;                            //temporary variables

   float distance;

   printf(“Enter X and Y coordinates of first point”);

   scanf(“%d %d”,&x1,&y1);

   printf(“Enter X and Y coordinates of Second point”);

   scanf(“%d %d”,&x2,&y2);

   x=x2-x1;

   y=y2-y1;

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

   printf(“Distance: %.2f”,distance);

   return 0;

}

Output:

Enter X and Y coordinates of first point

3   4

Enter X and Y coordinates of Second point

4   5

Distance: 1.414

Back to Programs list.