22.2k views
23 votes
Write a C program that gets 4 integer values from the user, namely, x1, x2, y1, and y2, and then calculates and returns the distance according to the above formula. Use whatever format for entering your data you like. Your program should prompt the user for these values, accept input from the keyboard, and then print out the result. You will have to execute the program four times to cover the test cases below (feel free to use a loop if you want to work ahead). Include a screenshot (or four) that shows the results for the following values: 1) (2,3), (4,5) 2) (4,3), (-6,-5) 3) (-1,3), (4,-2) 4) (-9,-12), (-7, -8)

User RanH
by
6.3k points

1 Answer

8 votes

Answer:

#include <stdio.h>

#include <math.h>

int main(){

float x1,x2,y1,y2,distance;

printf("x1: "); scanf("%f", &x1);

printf("y1: "); scanf("%f", &y1);

printf("x2: "); scanf("%f", &x2);

printf("y2: "); scanf("%f", &y2);

distance = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));

printf("Distance: %f", distance);

return 0;

}

Step-by-step explanation:

This line declares all required variable (i.e. the coordinates and the distance)

float x1,x2,y1,y2,distance;

The next four line prompt and get input for the coordinates

printf("x1: "); scanf("%f", &x1);

printf("y1: "); scanf("%f", &y1);

printf("x2: "); scanf("%f", &x2);

printf("y2: "); scanf("%f", &y2);

This calculates the distance

distance = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));

This prints the calculated distance

printf("Distance: %f", distance);

See attachment for sample runs

Write a C program that gets 4 integer values from the user, namely, x1, x2, y1, and-example-1
User Quentamia
by
7.0k points