Software & Finance





Visual C++ - Calculating Slope of a Line Given Two End Points





Here is the Visual C++ program for Calculating Slope of a Line Given Two End Points

 

It uses the following formula given points are (x1,y1) and (x2, y2)

 

slope = (y2 - y1) / (x2 - x1)

 


Source Code


#include <iostream>

#include <math.h>

 

void main()

{

    float slope;

    float x1, y1, x2, y2;

    float dx, dy;

 

    std::cout << "Program to find the slope of a line given two end points\n";

 

    std::cout << "Enter X1: ";

    std::cin >> x1;

 

    std::cout << "Enter Y1: ";

    std::cin >> y1;

 

    std::cout << "Enter X2: ";

    std::cin >> x2;

 

    std::cout << "Enter Y2: ";

    std::cin >> y2;

 

    dx = x2 - x1;

    dy = y2 - y1;

    slope = dy / dx;

 

    std::cout << "Slope of the line with end points (" << x1 << ", " << y1 << " and (" << x2 << ", " << y2 << ") = ";

    std::cout << slope << "\n";

 

}

Output


 

Program to find the slope of a line given two end points

Enter X1: 2.5

Enter Y1: 7.5

Enter X2: 12.5

Enter Y2: 18

Slope of the line with end points (2.5, 7.5 and (12.5, 18) = 1.05

Press any key to continue . . .