Program to find slope of a line

28 Jun, 2022
Given two coordinates, 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++
Java
Python
C#
PHP
Javascript
// C++ program for slope of line#include <bits/stdc++.h>using namespace std; // function to find the slope of a straight linefloat slope(float x1, float y1, float x2, float y2){    if (x2 - x1 != 0)        return (y2 - y1) / (x2 - x1);    return INT_MAX;} // driver code to check the above functionint main(){    float x1 = 4, y1 = 2;    float x2 = 2, y2 = 5;    cout << "Slope is: " << slope(x1, y1, x2, y2);    return 0;}
// Java program for slope of lineimport java.io.*; class GFG {    static float slope(float x1, float y1, float x2,                       float y2)    {        if (x2 - x1 != 0)            return (y2 - y1) / (x2 - x1);        return Integer.MAX_VALUE;    }    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));    }}
# Python program for slope of linedef slope(x1, y1, x2, y2):    if(x2 - x1 != 0):      return (float)(y2-y1)/(x2-x1)    return sys.maxint  # driver codex1 = 4y1 = 2x2 = 2y2 = 5print "Slope is :", slope(x1, y1, x2, y2)
using System; public static class GFG {    // C# program for slope of line     // function to find the slope of a straight line    public static float slope(float x1, float y1, float x2,                              float y2)    {        if (x2 - x1 != 0F) {            return (y2 - y1) / (x2 - x1);        }        return int.MaxValue;    }     // driver code to check the above function    internal static void Main()    {        float x1 = 4F;        float y1 = 2F;        float x2 = 2F;        float y2 = 5F;        Console.Write("Slope is: ");        Console.Write(slope(x1, y1, x2, y2));    }} // The code is contributed by Aarti_Rathi
<?php// PHP program for// slope of line // function to find the// slope of a straight linefunction slope($x1, $y1, $x2, $y2){  if($x1 == $x2)  {    return PHP_INT_MAX;  }       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 Sayan Chatterjee?>
// C Javascript program for slope of line // function to find the slope of a straight linefunction slope(x1, y1, x2, y2){    if (x2 - x1 != 0)    {        return (y2 - y1) / (x2 - x1);    }    return Number.MAX_VALUE;} // driver code to check the above functionvar x1 = 4;var y1 = 2;var x2 = 2;var y2 = 5;console.log("Slope is " + slope(x1, y1, x2, y2)); // The code is contributed by Aarti_Rathi
Slope is: -1.5
Time Complexity: O(1)Auxiliary Space: O(1)
vt_m
rohitsingh07052
susmitakundugoaldanga
iayushsrivastava1999
Rajput-Ji
vaishaligoyal878
famously
gabaa406
sayanc170
_shinchancode
adi1212
Geometric
School Programming
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Optimum location of point to minimize total distance
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Check whether a given point lies inside a triangle or not
Program for Point of Intersection of Two Lines
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java