Problem: Write a C++ program to find the length and gradient of the line joining the points (2,6) and (-3, -5) by taking user input.

Code: 

#include <iostream>
#include <cmath>

using namespace std;

int main(){
    float x1, x2, y1, y2, dist, grad;

    cout << "This program determines distance and gradient between two points\n";



    cout << "Enter the first coordinate: \n";
    cout << "x1 = ";
    cin >> x1;
    cout << "y1 = ";
    cin >> y1;
    cout << "Enter the second coordinate: \n";
    cout << "x2 = ";
    cin >> x2;
    cout << "y2 = ";
    cin >> y2;

    dist = sqrt(pow((x2-x1),2)+pow((y2-y1),2));

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

    cout << "The length is " << dist << " and The gradient is " << grad;

}

Output: 

This program determines distance and gradient between two points
Enter the first coordinate:
x1 = 2
y1 = 6
Enter the second coordinate:
x2 = -3
y2 = -5
The length is 12.083 and The gradient is 2.2
Process returned 0 (0x0)   execution time : 5.444 s
Press any key to continue.

Previous Post Next Post