218k views
2 votes
Write a C program to calculate and display the coordinates of midpoint - M of a linesegment between two given points - say A and B, the slope of the line - s and the distance b/wthe points A and B given by d. Your program should prompt the user to enter floating pointvalues for x and y coordinates for the two points - A (x1, y1) and B (x2, y2). Then it calculatesthe floating point coordinates of the midpoint - M (xm, ym) using the formula -Xm

User Fbozo
by
7.0k points

1 Answer

5 votes

Answer:

//Program in C.

// header file

#include <stdio.h>

#include <math.h>

// main function

int main(void) {

// variables

float x1,y1,x2,y2;

float s,xm,ym,d;

// ask to enter x coordinate of A

printf("Enter coordinate (x) of point A:");

// read x1

scanf("%f",&x1);

// ask to enter y coordinate of A

printf("Enter coordinate (y) of point A:");

// read y1

scanf("%f",&y1);

// ask to enter x coordinate of B

printf("Enter coordinate (x) of point B:");

//read x2

scanf("%f",&x2);

// ask to enter y coordinate of B

printf("Enter coordinate (y) of point B:");

// read y2

scanf("%f",&y2);

// calculate Midpoint of A and B

xm=(x1+x2)/2;

ym=(y1+y2)/2;

// calculate slope of the line

s=(y2-y1)/(x2-x1);

// calculate Distance between two points

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

// print result

printf("Midpoint of both the point is: %f %f",xm,ym);

printf("\\Slope of both the line is: %f",s);

printf("\\Distance between both the point is: %f",d);

return 0;

}

Step-by-step explanation:

Read the x & y coordinates of both the points.Then calculate the Midpoint of both the and assign it to variables "xm"&"ym".Then calculate the slope of the line and assign it to variable "s".Then find the distance between the two points and assign it to variable "d".Print all these results.

Output:

Enter coordinate (x) of point A:2

Enter coordinate (y) of point A:3

Enter coordinate (x) of point B:9

Enter coordinate (y) of point B:8

Midpoint of both the point is: 5.500000 5.500000

Slope of both the line is: 0.714286

Distance between both the point is: 8.602325

User Maxim Yefremov
by
6.2k points