Software & Finance





Turbo C - Distance between the two points





Here is the Turbo C program for the Distance between the two points.

 

It uses the following formula give points are (x1,y1) and (x2, y2)

 

SQRT( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) )

 


Source Code


#include <stdio.h>

#include <math.h>

 

void main()

{

    float distance;

    int x1, y1, x2, y2;

    int dx, dy;

 

    printf("Program for distance between the two points\n");

 

    printf("Enter X1: ");

    scanf("%d", &x1);

 

    printf("Enter Y1: ");

    scanf("%d", &y1);

 

    printf("Enter X2: ");

    scanf("%d", &x2);

 

    printf("Enter Y2: ");

    scanf("%d", &y2);

 

    dx = x2 - x1;

    dy = y2 - y1;

 

    distance = sqrt(dx*dx + dy*dy);

    printf("%.4f", distance);

}

Output


 

Program for distance between the two points

Enter X1: 10

Enter Y1: 10

Enter X2: 30

Enter Y2: 30

Distance between (10, 10) and (30, 30) = SQRT(800) = 28.2843