Check whether two straight lines are parallel or not
Last Updated :
07 Feb, 2022
Given equations of two lines (a1, b1, c1) and (a2, b2, c2) such that (ai, bi, ci) are the coefficients of X2, X and a constant term of the straight line respectively, in the general equation , the task is to check if both the straight lines are parallel or not. If they are found to be parallel, then print “Yes”. Otherwise, print “No”.
Examples:
Input: a1 = -2, b1 = 4, a2 = -6, b2 = 12
Output: Yes
Explanation:
The slope of both lines are equal i.e., a1/b1 = a2/ b2 = -2.
Input: a1 = 11, b1 = 3, a2 = 7, b2 = -10
Output: No
Explanation:
The slope of both lines are not equal i.e., a1/b1? a2/b2.
Approach: To check if two lines are parallel to each other or not, the idea is to compare the slope of the given lines. If the slope of the given lines is equal then the given lines are parallel. Therefore, print “Yes” else print “No”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void parallel( float a1, float b1,
float c1, float a2,
float b2, float c2)
{
if (a1*b2 == a2*b1) {
cout << "Yes" ;
}
else {
cout << "No" ;
}
}
int main()
{
float a1 = -2, b1 = 4, c1 = 5;
float a2 = -6, b2 = 12, c2 = 6;
parallel(a1, b1, c1, a2, b2, c2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void parallel( float a1, float b1,
float c1, float a2,
float b2, float c2)
{
if (a1*b2 == a2*b1)
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
public static void main(String args[])
{
float a1 = - 2 , b1 = 4 , c1 = 5 ;
float a2 = - 6 , b2 = 12 , c2 = 6 ;
parallel(a1, b1, c1, a2, b2, c2);
}
}
|
Python3
def parallel(a1, b1, c1, a2, b2, c2):
if a1 * b2 = = a2 * b1:
print ( "Yes" );
else :
print ( "No" );
if __name__ = = '__main__' :
a1 = - 2 ; b1 = 4 ; c1 = 5 ;
a2 = - 6 ; b2 = 12 ; c2 = 6 ;
parallel(a1, b1, c1, a2, b2, c2);
|
C#
using System;
class GFG
{
static void parallel( float a1, float b1,
float c1, float a2,
float b2, float c2)
{
if (a1*b2 == a2*b1)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
public static void Main()
{
float a1 = -2, b1 = 4, c1 = 5;
float a2 = -6, b2 = 12, c2 = 6;
parallel(a1, b1, c1, a2, b2, c2);
}
}
|
Javascript
<script>
function parallel(a1, b1,
c1, a2,
b2, c2)
{
if (a1*b2==a2*b1)
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
}
let a1 = -2, b1 = 4, c1 = 5;
let a2 = -6, b2 = 12, c2 = 6;
parallel(a1, b1, c1, a2, b2, c2);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...