157k views
3 votes
Write a program that prompt the user to enter the coordinate of two points (x1, y1) and (x2,y2), and displays the slope of the line that connects the two points.

User Nelu
by
6.6k points

1 Answer

6 votes

Answer:

Here is code in C++.

//include headers

#include <bits/stdc++.h>

using namespace std;

//main function

int main() {

//variables to store coordinates

float x1,x2,y1,y2;

cout<<"Please Enter the coordinate of first point (x1,y1): ";

// reading coordinate of first point

cin>>x1>>y1;

cout<<"Please Enter the coordinate of second point (x2,y2): ";

// reading coordinate of second point

cin>>x2>>y2;

//calculating Slope of the line that connects these points

float m=(x2-x1)/(y2-y1);

cout<<"Slope of the line that connects these two points is : "<<m<<endl;

}

Step-by-step explanation:

Declare four variables x1,x2,y1,y2 to store the coordinate of both points.

Read the coordinate of both the point from user. Calculate the slop of the

line which connects these two points with the formula m=(x2-x1)/(y2-y1).

Output:

Please Enter the coordinate of first point (x1,y1): 1 3

Please Enter the coordinate of second point (x2,y2): 5 12

Slope of the line that connects these two points is : 0.444444

User Nnesterov
by
6.7k points