Write a complete C program that prompts the user for the coordinates of two 3- D points ( x1 , y1 , z1 ) and ( x2 , y2 , z2 ) and displays the distance between them computed using the following formula:
distance= square root of (x1-x2)2+(y1-y2)2+(z1-z2)2
#include <stdio.h>
#include <math.h>
int main()
{
int x1,x2,y1,y2,z1,z2;
double distance;
printf("Enter the first 3-D point: ");
scanf("%d %d %d", &x1, &y1, &z1);
printf("Enter the second 3-D point: ");
scanf("%d %d %d", &x2, &y2, &z2);
distance = sqrt((x2-x1) * (x2-x1) + (y2-y1) *(y2-y1) +(z2-z1) *
(z2-z1));
printf("Distance is %ld\n", distance);
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the first 3-D point: 1 2 3
Enter the second 3-D point: 4 5 6
Distance is 3
Get Answers For Free
Most questions answered within 1 hours.