Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Check two lines are parallel or not

C Basic Declarations and Expressions: Exercise-138 with Solution

Write a C program to test whether two lines are parallel or not. The four points are P(x1, y1), Q(x2, y2), R(x3, y3) and S(x4, y4), check PQ and RS are parallel are not.

Input:
−100 <= x1, y1, x2, y2, x3, y3, x4, y4 <= 100
Each value is a real number with at most 5 digits after the decimal point.

Sample Solution:

C Code:

#include <stdio.h>
main() {
  double x1, x2, y1, y2, x3, y3, x4, y4;
  int i, n;

  printf("Input P(x1,y1):\n");
  scanf("%lf %lf", &x1, &y1);
  printf("\nInput P(x2,y2):\n");
  scanf("%lf %lf", &x2, &y2);
  printf("\nInput P(x3,y3):\n");
  scanf("%lf %lf", &x3, &y3);
  printf("\nInput P(x4,y4):\n");
  scanf("%lf %lf", &x4, &y4);

  if ((x1 == x2) && (x3 == x4))
    printf("\nPQ and RS are parallel!\n");
  else if ((x1 == x2) || (x3 == x4))
    printf("\nPQ and RS are not parallel!\n");
  else if (((y1 - y2) / (x1 - x2) - (y3 - y4) / (x3 - x4)) == 0.0)
    printf("\nPQ and RS are parallel!\n");
  else
    printf("\nPQ and RS are not parallel!\n");
  return (0);
}

Sample Output:

Input P(x1,y1):
5
7

Input P(x2,y2):
3
6

Input P(x3,y3):
8
9

Input P(x4,y4):
5
6

PQ and RS are not parallel!

Flowchart:

C Programming Flowchart: Check two lines are parallel or not.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a C program to check if a point (x, y) is within a triangle or not. The triangle has formed by three points.
Next: Write a C program to find the maximum sum of a contiguous subsequence from a given sequence of numbers a1, a2, a3, ... an ( n = number of terms in the sequence).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP