Given two co-ordinates, find the slope of a straight line.

Examples:

Input : x1 = 4, y1 = 2, x2 = 2, y2 = 5 Output : Slope is -1.5

**Approach:** To calculate the slope of a line you need only two points from that line, (x1, y1) and (x2, y2). The equation used to calculate the slope from two points is:

*Below is the implementation of the above approach:*

## c++

`// C program for slope of line ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find the slope of a straight line ` `float` `slope(` `float` `x1, ` `float` `y1, ` `float` `x2, ` `float` `y2) ` `{ ` ` ` `return` `(y2 - y1) / (x2 - x1); ` `} ` ` ` `// driver code to check the above function ` `int` `main() ` `{ ` ` ` `float` `x1 = 4, y1 = 2; ` ` ` `float` `x2 = 2, y2 = 5; ` ` ` `cout << ` `"Slope is: "` ` ` `<< slope(x1, y1, x2, y2); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for slope of line ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `static` `float` `slope(` `float` `x1, ` `float` `y1, ` ` ` `float` `x2, ` `float` `y2) ` ` ` `{ ` ` ` `return` `(y2 - y1) / (x2 - x1); ` ` ` `} ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `float` `x1 = ` `4` `, y1 = ` `2` `; ` ` ` `float` `x2 = ` `2` `, y2 = ` `5` `; ` ` ` `System.out.println(` `"Slope is "` `+ ` ` ` `slope(x1, y1, x2, y2)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python program for slope of line ` `def` `slope(x1, y1, x2, y2): ` ` ` `return` `(` `float` `)(y2` `-` `y1)` `/` `(x2` `-` `x1) ` ` ` `# driver code ` `x1 ` `=` `4` `y1 ` `=` `2` `x2 ` `=` `2` `y2 ` `=` `5` `print` `"Slope is :"` `, slope(x1, y1, x2, y2) ` |

*chevron_right*

*filter_none*

## C#

`// C# program for slope of line ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `float` `slope(` `float` `x1, ` `float` `y1, ` ` ` `float` `x2, ` `float` `y2) ` ` ` `{ ` ` ` `return` `(y2 - y1) / (x2 - x1); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `float` `x1 = 4, y1 = 2; ` ` ` `float` `x2 = 2, y2 = 5; ` ` ` `Console.WriteLine(` `"Slope is "` `+ ` ` ` `slope(x1, y1, x2, y2)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for ` `// slope of line ` ` ` `// function to find the ` `// slope of a straight line ` `function` `slope(` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `) ` `{ ` ` ` `return` `(` `$y2` `- ` `$y1` `) / ` ` ` `(` `$x2` `- ` `$x1` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$x1` `= 4; ` ` ` `$y1` `= 2; ` ` ` `$x2` `= 2; ` ` ` `$y2` `= 5; ` ` ` `echo` `"Slope is: "` ` ` `, slope(` `$x1` `, ` `$y1` `, ` ` ` `$x2` `, ` `$y2` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Slope is: -1.5

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Slope of the line parallel to the line with the given slope
- Find points at a given distance on a line of given slope
- Slope of perpendicular to line
- Program to find line passing through 2 Points
- Program to find the mid-point of a line
- Given n line segments, find if any two segments intersect
- Find an Integer point on a line segment with given two ends
- Find the other end point of a line with given one end and mid
- Find foot of perpendicular from a point in 2 D plane to a Line
- Find Partition Line such that sum of values on left and right is equal
- How to check if two given line segments intersect?
- Count maximum points on same line
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Represent a given set of points by the best possible straight line
- Reflection of a point about a line in C++
- Check if a line touches or intersects a circle
- Direction of a Point from a Line Segment
- One line function for factorial of a number
- Section formula (Point that divides a line in given ratio)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.