[ Submission site | Show solution | Hide solution ]
Write a C program that reads the coordinates of the three vertices of a triangle ABC
and determines whether the triangle is equilateral or isosceles or neither. In case the
triangle is isosceles, your program should also report the two sides that are of equal length.
Submit the output of your program on the following triangles:
(1,1), (4,-3), (8,0)
(1,1), (4,3), (8,0)
(1.1,-2.1), (6.6,6.8), (10.0,-7.6)
(2,-2), (-4,4), (-2,0)
(0,0), (10,0), (5,5*sqrt(3))
Sample runs
Point A: 0,0
Point B: 5,0
Point C: 0,-5
The triangle is isosceles with |AB| = |AC|.
Point A: 2,3
Point B: 3,5
Point C: 5,8
The triangle is neither isosceles nor equilateral.
Mathematical challenge (not for submission)
Try to locate a non-trivial triangle for which your program outputs
"The triangle is equilateral".
Solution
#include <stdio.h>
int main ()
{
double ax, ay, bx, by, cx, cy;
double dab, dac, dbc;
printf("Point A: "); scanf("%lf,%lf", &ax, &ay);
printf("Point B: "); scanf("%lf,%lf", &bx, &by);
printf("Point C: "); scanf("%lf,%lf", &cx, &cy);
dab = (ax-bx)*(ax-bx) + (ay-by)*(ay-by);
dac = (ax-cx)*(ax-cx) + (ay-cy)*(ay-cy);
dbc = (bx-cx)*(bx-cx) + (by-cy)*(by-cy);
if (dab == dac) {
if (dac == dbc) printf("The triangle is equilateral.\n");
else printf("The triangle is isosceles with |AB| = |AC|.\n");
} else if (dac == dbc) {
printf("The triangle is isosceles with |AC| = |BC|.\n");
} else if (dab == dbc) {
printf("The triangle is isosceles with |AB| = |BC|.\n");
} else {
printf("The triangle is neither isosceles nor equilateral.\n");
}
}
Output
Point A: 1,1
Point B: 4,-3
Point C: 8,0
The triangle is isosceles with |AB| = |BC|.
Point A: 1,1
Point B: 4,3
Point C: 8,0
The triangle is neither isosceles nor equilateral.
Point A: 1.1,-2.1
Point B: 6.6,6.8
Point C: 10.0,-7.6
The triangle is isosceles with |AB| = |AC|.
Point A: 2,-2
Point B: -4,4
Point C: -2,0
The triangle is isosceles with |AC| = |BC|.
Point A: 0,0
Point B: 10,0
Point C: 5,8.660254037844386467637231707529361834714026269051903140279034897
The triangle is isosceles with |AC| = |BC|.
[ Submission site | Show solution | Hide solution ]