Mar 11, 2020

C program 02





2. Find the distance between two points.

The Distance Formula between Two Points is derived from the Pythagoras Theorem.
According to the Pythagoras Theorem,

(Hypotenuse)2 = (Base)2 + (Height)2

Example: z2 = x2 + y2
Formula To Calculate Distance Between Two Points in C Programming
Distance Between Two Points =  √ (x1 – y1)2 + (x2 – y2)2


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    float x1, x2, y1, y2, distance;
    printf("\nEnter The Coordinates of Point A:\n");
    printf("\nX - Axis Coordinate: \t");
    scanf("%f", &x1);
    printf("\nY - Axis Coordinate: \t");
    scanf("%f", &x2);

    printf("\nEnter The Coordinates of Point B:\n");
    printf("\nY - Axis Coordinate:\t");
    scanf("%f", &y1);
    printf("\nY - Axis Coordinate: \t");
    scanf("%f", &y2);

    distance = sqrt((x1 - y1) * (x1 - y1) + (x2 - y2) * (x2 - y2));
    printf("\nDistance between Points A and B: %f\n", distance);
}


Output

 

No comments:

Post a Comment