Program to find slope of a line in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find slope of a line.

For this we will be provided with two coordinates on the line. Our task is to find the slope of the line connecting those two coordinates.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//finding slope of line
float slope(float x1, float y1, float x2, float y2) {
   return (y2 - y1) / (x2 - x1);
}
int main() {
   float x1 = 4, y1 = 2;
   float x2 = 2, y2 = 5;
   cout << "Slope is: " << slope(x1, y1, x2, y2);
   return 0;
}

Output

Slope is: -1.5
raja
Published on 19-May-2020 15:26:29
Advertisements