Check whether the point (x, y) lies on a given line
Last Updated :
07 Jan, 2024
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++
#include <bits/stdc++.h>
using namespace std;
bool pointIsOnLine( int m, int c, int x, int y)
{
if (y == ((m * x) + c))
return true ;
return false ;
}
int main()
{
int m = 3, c = 2;
int x = 1, y = 5;
if (pointIsOnLine(m, c, x, y))
cout << "Yes" ;
else
cout << "No" ;
}
|
Java
class GFG
{
static boolean pointIsOnLine( int m, int c,
int x, int y)
{
if (y == ((m * x) + c))
return true ;
return false ;
}
public 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" );
}
}
|
Python3
def pointIsOnLine(m, c, x, y):
if (y = = ((m * x) + c)):
return True ;
return False ;
m = 3 ; c = 2 ;
x = 1 ; y = 5 ;
if (pointIsOnLine(m, c, x, y)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static bool pointIsOnLine( int m, int c,
int x, int y)
{
if (y == ((m * x) + c))
return true ;
return false ;
}
public 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" );
}
}
|
Javascript
<script>
function pointIsOnLine(m, c, x, y)
{
if (y == ((m * x) + c))
return true ;
return false ;
}
var m = 3, c = 2;
var x = 1, y = 5;
if (pointIsOnLine(m, c, x, y))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function pointIsOnLine( $m , $c , $x , $y )
{
if ( $y == (( $m * $x ) + $c ))
return true;
return false;
}
$m = 3; $c = 2;
$x = 1; $y = 5;
if (pointIsOnLine( $m , $c , $x , $y ))
echo "Yes" ;
else
echo "No" ;
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...