Check whether the point (x, y) lies on a given line - GeeksforGeeks

22 Oct, 2021
Given the values of m and c for the equation of a line y = (m * x) + c, the task is to find whether the point (x, y) lies on the given line.
Examples: 
Input: m = 3, c = 2, x = 1, y = 5 Output: Yes m * x + c = 3 * 1 + 2 = 3 + 2 = 5 which is equal to y Hence, the given point satisfies the line’s equation
Input: m = 5, c = 2, x = 2, y = 5 Output: No  
Approach: In order for the given point to lie on the line, it must satisfy the equation of the line. Check whether y = (m * x) + c holds true.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that return true if// the given point lies on the given linebool pointIsOnLine(int m, int c, int x, int y){    // If (x, y) satisfies the equation of the line    if (y == ((m * x) + c))        return true;     return false;} // Driver codeint main(){    int m = 3, c = 2;    int x = 1, y = 5;     if (pointIsOnLine(m, c, x, y))        cout << "Yes";    else        cout << "No";}
// Java implementation of the approach class GFG{ // Function that return true if// the given point lies on the given linestatic boolean pointIsOnLine(int m, int c,                        int x, int y){    // If (x, y) satisfies the equation    // of the line    if (y == ((m * x) + c))        return true;     return false;} // Driver codepublic static void main(String[] args){    int m = 3, c = 2;    int x = 1, y = 5;     if (pointIsOnLine(m, c, x, y))        System.out.print("Yes");    else        System.out.print("No");}} // This code has been contributed by 29AjayKumar
# Python3 implementation of the approach # Function that return true if the# given point lies on the given linedef pointIsOnLine(m, c, x, y):         # If (x, y) satisfies the    # equation of the line    if (y == ((m * x) + c)):        return True;     return False; # Driver codem = 3; c = 2;x = 1; y = 5; if (pointIsOnLine(m, c, x, y)):    print("Yes");else:    print("No");     # This code is contributed by mits
// C# implementation of the approachusing System; class GFG{ // Function that return true if// the given point lies on the given linestatic bool pointIsOnLine(int m, int c,                          int x, int y){    // If (x, y) satisfies the equation    // of the line    if (y == ((m * x) + c))        return true;     return false;} // Driver codepublic static void Main(){    int m = 3, c = 2;    int x = 1, y = 5;     if (pointIsOnLine(m, c, x, y))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by Akanksha Rai
<?php// PHP implementation of the approach // Function that return true if the// given point lies on the given linefunction pointIsOnLine($m, $c, $x, $y){         // If (x, y) satisfies the equation    // of the line    if ($y == (($m * $x) + $c))        return true;     return false;} // Driver code$m = 3; $c = 2;$x = 1; $y = 5; if (pointIsOnLine($m, $c, $x, $y))    echo "Yes";else    echo "No";     // This code is contributed by Ryuga?>
<script> // Javascript implementation of the approach // Function that return true if// the given point lies on the given linefunction pointIsOnLine(m, c, x, y){         // If (x, y) satisfies the equation    // of the line    if (y == ((m * x) + c))        return true;     return false;} // Driver codevar m = 3, c = 2;var x = 1, y = 5; if (pointIsOnLine(m, c, x, y))    document.write("Yes");else    document.write("No"); // This code is contributed by Rajput-Ji </script>
Yes
 
ankthon
Akanksha_Rai
Mithun Kumar
29AjayKumar
Rajput-Ji
ankita_saini
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Equation of circle when three points on the circle are given
Convex Hull using Divide and Conquer Algorithm
Circle and Lattice Points
Line Clipping | Set 2 (Cyrus Beck Algorithm)
Check if a right-angled triangle can be formed by the given coordinates
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7