{"id":16898,"date":"2021-08-17T08:51:20","date_gmt":"2021-08-17T03:21:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=16898"},"modified":"2021-11-22T18:37:21","modified_gmt":"2021-11-22T13:07:21","slug":"python-program-to-check-if-two-lines-are-parallel-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-two-lines-are-parallel-or-not\/","title":{"rendered":"Python Program to Check if two Lines are Parallel or Not"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Greatest Digit in a Number.<\/a> If two lines remain the same distance apart along their entire length, they are said to be parallel. They will not meet no matter how far you stretch them. These lines are denoted by the equations ax+by=c.<\/p>\n The line equation is ax+by=c, where an is the x coefficient and b is the y coefficient. If the slopes of two lines are equal, we say they are parallel. As a result, we must determine the slope, which is “rise over run.”<\/p>\n The straight-line equation is y=mx+c, where m is the slope. Take a1,b1,c1 and a2,b2,c2 from the user and see if they are parallel.<\/p>\n Given the values of equations of two lines, and the task is to check if the given two lines are parallel or not.<\/p>\n Examples:<\/strong><\/p>\n Example 1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example 2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check if the given two lines are parallel or not.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n They are parallel to each other<\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Find the Greatest Digit in a Number. Parallel Lines : If two lines remain the same distance apart along their entire length, they are said to be parallel. They will not meet no matter how far you stretch them. These lines are denoted by the …<\/p>\n
\nParallel Lines :<\/strong><\/p>\na1=4, b1=8, c1=13\r\na2=2, b2=4, c2=7<\/pre>\n
The given lines are parallel to each other<\/pre>\n
a1=8, b1=0, c1=9\r\na2=8, b2=0, c2=11<\/pre>\n
The given lines are parallel to each other<\/pre>\n
Program to Check if two Lines are Parallel or Not<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
#Create a function checkParallel() which accepts the 6 parameters\r\n#(First and second line x,y,z coordinates)\r\n#We can Check if the slopes of the given two lines are equal by formula (a1\/b1 == a2\/b2).\r\ndef checkParallel(a1,b1,c1,a2,b2,c2):\r\n #Inside the checkParallel() function.\r\n #Check if both lines y coordinates are not equal to 0 \r\n #using the If conditional Statement.\r\n if(b1!=0 and b2!=0):\r\n #If it is true then check a1\/b1 is equal to a2\/b2 \r\n #using another Nested If conditional Statement.\r\n if(a1\/b1==a2\/b2):\r\n #If it is true then return True\r\n return True\r\n else:\r\n #Else return False.\r\n return False\r\n #If parent If conditional statement is false then inside the else statement \r\n \r\n else:\r\n #check if first line and second line x and y coordinates are equal\r\n #or not using the nested If conditional statement.\r\n if(a1==a2 and b1==b2):\r\n \t #If it is true then return True else return False.\r\n return True\r\n else:\r\n return False\r\n#Give the values of a1,b1,c1 as static input \r\n#and store it in three separate variables.\r\na1,b1,c1=4,8,13\r\n#Give the values of a2,b2,c2 as static input \r\n#and store it in three separate variables.\r\na2,b2,c2=2,4,7\r\n#Pass the a1,b1,c1,a2,b2,c2 as the arguments to checkParallel() function\r\n#inside the if Conditional statement.\r\nif(checkParallel(a1,b1,c1,a2,b2,c2)):\r\n #If it is true then print those lines are parallel.\r\n print('The given lines are parallel to each other')\r\nelse:\r\n #Else they are not parallel.\r\n print('The given lines are not parallel to each other')\r\n<\/pre>\n
The given lines are parallel to each other<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
#Create a function checkParallel() which accepts the 6 parameters\r\n#(First and second line x,y,z coordinates)\r\n#We can Check if the slopes of the given two lines are equal by formula (a1\/b1 == a2\/b2).\r\ndef checkParallel(a1,b1,c1,a2,b2,c2):\r\n #Inside the checkParallel() function.\r\n #Check if both lines y coordinates are not equal to 0 \r\n #using the If conditional Statement.\r\n if(b1!=0 and b2!=0):\r\n #If it is true then check a1\/b1 is equal to a2\/b2 \r\n #using another Nested If conditional Statement.\r\n if(a1\/b1==a2\/b2):\r\n #If it is true then return True\r\n return True\r\n else:\r\n #Else return False.\r\n return False\r\n #If parent If conditional statement is false then inside the else statement \r\n \r\n else:\r\n #check if first line and second line x and y coordinates are equal\r\n #or not using the nested If conditional statement.\r\n if(a1==a2 and b1==b2):\r\n #If it is true then return True else return False.\r\n return True\r\n else:\r\n return False\r\n#Give the values of a1,b1,c1 as user input using map(),int(),split() functions \r\n#and store it in three separate variables.\r\na1,b1,c1=map(int,input('Enter some random x y z coordinates separated by spaces = ').split()) \r\n#Give the values of a2,b2,c2 as user input using map(),int(),split() functions \r\n#and store it in three separate variables.\r\na2,b2,c2=map(int,input('Enter some random x y z coordinates separated by spaces = ').split())\r\n#Pass the a1,b1,c1,a2,b2,c2 as the arguments to checkParallel() function\r\n#inside the if Conditional statement.\r\nif(checkParallel(a1,b1,c1,a2,b2,c2)):\r\n #If it is true then print those lines are parallel.\r\n print('The given lines are parallel to each other')\r\nelse:\r\n #Else they are not parallel.\r\n print('The given lines are not parallel to each other')<\/pre>\n
Enter some random x y z coordinates separated by spaces = 8 0 9\r\nEnter some random x y z coordinates separated by spaces = 8 0 11\r\nThe given lines are parallel to each other<\/pre>\n
\n