Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.
INPUT:
Six integers x1, y1, x2, y2, x3, y3 separated by whitespace.
INPUT:
Six integers x1, y1, x2, y2, x3, y3 separated by whitespace.
OUTPUT:
Print “Yes” if all the points fall on straight line, “No” otherwise.
Print “Yes” if all the points fall on straight line, “No” otherwise.
CONSTRAINTS:
-1000 <= x1, y1, x2, y2, x3, y3 <= 1000
Solution:
-1000 <= x1, y1, x2, y2, x3, y3 <= 1000
Solution:
#include<stdio.h>
int main() {
int x1,y1,x2,y2,x3,y3;
//Reading all 6 integers from the input using scanf()
scanf("%d %d %d %d %d %d", &x1,&y1,&x2,&y2,&x3,&y3);
//Checking if the slopes (y2-y1)/(x2-x1) == (y3-y2)/(x3-x2). If they are equal, then all the points lie on the same line.
//Instead of performing division in LHS and RHS, we cross multiply (x2-x1) and (x3-x2) to handle the case when either (x2-x1) = 0 or (x3-x2) = 0.
if ((y2-y1)*(x3-x2) == (y3-y2)*(x2-x1)) {
printf("Yes");
} else {
printf("No");
}
return 0;
}
int main() {
int x1,y1,x2,y2,x3,y3;
//Reading all 6 integers from the input using scanf()
scanf("%d %d %d %d %d %d", &x1,&y1,&x2,&y2,&x3,&y3);
//Checking if the slopes (y2-y1)/(x2-x1) == (y3-y2)/(x3-x2). If they are equal, then all the points lie on the same line.
//Instead of performing division in LHS and RHS, we cross multiply (x2-x1) and (x3-x2) to handle the case when either (x2-x1) = 0 or (x3-x2) = 0.
if ((y2-y1)*(x3-x2) == (y3-y2)*(x2-x1)) {
printf("Yes");
} else {
printf("No");
}
return 0;
}
No comments:
Post a Comment