Program: 78
Give three points (x1, y1), (x2, y2) and (x3, y3), write a c program to check if all the three points fall on one straight line.
#include<stdio.h> #include<conio.h> #include<math.h> int main() { int x1, y1, x2, y2, x3, y3; double ab, bc, ac, abc; printf("Enter the co-ordinates of first point (X1, Y1): "); scanf("%d %d", &x1, &y1); printf("Enter the co-ordinates of second point (X2, Y2): "); scanf("%d %d", &x2, &y2); printf("Enter the co-ordinates of third point (X3, Y3): "); scanf("%d %d", &x3, &y3); //suppose we have three points a, b, c //then all these points fall on one straight line if and only if //ab + bc = ac (distance should be same) ab = sqrt(pow(x2-x1,2)+pow(y2-y1,2)); bc = sqrt(pow(x3-x2,2)+pow(y3-y2,2)); ac = sqrt(pow(x3-x1,2)+pow(y3-y1,2)); printf("ab: %f\t bc: %f\t ac: %f\n",ab, bc, ac); abc = ab+bc; if(abc==ac) { printf("ab + bc = ac\n"); printf("All the three points fall on one straight line."); } else printf("All the three points are not present on one straight line."); return 0; }
Output:
Enter the co-ordinates of first point (X1, Y1): -3 4
Enter the co-ordinates of second point (X2, Y2): 3 2
Enter the co-ordinates of third point (X3, Y3): 6 1
ab: 6.324555 bc: 3.162278 ac: 9.486833
ab + bc = ac
All the three points fall on one straight line.
Leave a Comment