Check whether two straight lines are parallel or not - GeeksforGeeks

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 = 12Output: YesExplanation:The slope of both lines are equal i.e., a1/b1 = a2/ b2 = -2.
Input: a1 = 11, b1 = 3, a2 = 7, b2 = -10Output: NoExplanation: 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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to check if two lines// are parallel or notvoid parallel(float a1, float b1,              float c1, float a2,              float b2, float c2){    // If slopes are equal    // then -(a1 / b1) = -(a2 / b2)    // which is a1*b2 = a2*b1    if (a1*b2 == a2*b1) {        cout << "Yes";    }    else {        cout << "No";    }} // Driver Codeint main(){    float a1 = -2, b1 = 4, c1 = 5;    float a2 = -6, b2 = 12, c2 = 6;     // Function Call    parallel(a1, b1, c1, a2, b2, c2);     return 0;}
// Java program to implement// the above approachimport java.util.*;class GFG{ // Function to check if two lines// are parallel or notstatic void parallel(float a1, float b1,              float c1, float a2,              float b2, float c2){       // If slopes are equal    // then (-(a1 / b1)) == (-(a2 / b2))    // which is a1*b2 = a2*b1    if (a1*b2 == a2*b1)    {        System.out.println("Yes");    }    else    {         System.out.println("No");    }} // Driver Codepublic static void main(String args[]){    float a1 = -2, b1 = 4, c1 = 5;    float a2 = -6, b2 = 12, c2 = 6;     // Function Call    parallel(a1, b1, c1, a2, b2, c2);}} // This code is contributed by splevel62.
# Python program to implement# the above approach  # Function to check if two lines# are parallel or notdef parallel(a1, b1, c1, a2, b2, c2):       # If slopes are equal    # then ((-(a1 / b1)) == (-(a2 / b2)))    # which is a1*b2 = a2*b1    if a1*b2==a2*b1:        print("Yes");    else:        print("No"); # Driver Codeif __name__ == '__main__':    a1 = -2; b1 = 4; c1 = 5;    a2 = -6; b2 = 12; c2 = 6;     # Function Call    parallel(a1, b1, c1, a2, b2, c2); # This code is contributed by 29AjayKumar
// C# program to implement// the above approachusing System;class GFG{   // Function to check if two lines// are parallel or notstatic void parallel(float a1, float b1,              float c1, float a2,              float b2, float c2){       // If slopes are equal    // then (-(a1 / b1)) == (-(a2 / b2))    // which is a1*b2 = a2*b1    if  (a1*b2 == a2*b1)     {        Console.Write("Yes");    }    else    {         Console.Write("No");    }} // Driver Codepublic static void Main(){    float a1 = -2, b1 = 4, c1 = 5;    float a2 = -6, b2 = 12, c2 = 6;     // Function Call    parallel(a1, b1, c1, a2, b2, c2);}} // This code is contributed by susmitakundugoaldanga.
<script>// javascript program of the above approach // Function to check if two lines// are parallel or notfunction parallel(a1, b1,              c1, a2,              b2, c2){       // If slopes are equal    // then (-(a1 / b1)) == (-(a2 / b2))    // which is a1*b2==a2*b1    if (a1*b2==a2*b1)    {        document.write("Yes");    }    else    {         document.write("No");    }}     // Driver Code         let a1 = -2, b1 = 4, c1 = 5;    let a2 = -6, b2 = 12, c2 = 6;     // Function Call    parallel(a1, b1, c1, a2, b2, c2); </script>
Yes
Time Complexity: O(1)Auxiliary Space: O(1)
splevel62
susmitakundugoaldanga
29AjayKumar
chinmoy1997pal
DevarshiAgarwal
Geometric-Lines
Technical Scripter 2020
Geometric
Mathematical
Technical Scripter
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Haversine formula to find distance between two points on a sphere
Program to find slope of a line
Equation of circle when three points on the circle are given
Program to find line passing through 2 Points
Maximum Manhattan distance between a distinct pair from N coordinates
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7