Classify a triangle from its three sides, using several conditions in the right order.
What to build
A program that names the kind of a triangle.
Requirements
- Read three side lengths.
- Print
invalidif they cannot form a triangle (the two smaller sides must add up to more than the biggest). - Otherwise print
equilateral(3 equal sides),isosceles(exactly 2 equal sides) orscalene(no equal sides). If the triangle has a right angle, add a space and the wordrightafter the name.
Input
One line: three integers.
Output
One line.
Constraints
1 ≤ each side ≤ 1000.
Example
Input:
3 4 5
Output:
scalene right
Why it works
Sorting the sides makes both the triangle rule and the right-angle test a check on the last side.
Examples
Example 1 · Input
3 4 5
Expected output
scalene right
Example 2 · Input
5 5 8
Expected output
isosceles
Hints
Hint 1
Sort the three sides first, so the biggest is last.
Hint 2
It is a triangle only when the two smaller sides add up to more than the biggest. A right triangle has a squared plus b squared equal to c squared.
Solution
Show one correct solution (try it yourself first)
<?php
fscanf(STDIN, "%d %d %d", $a, $b, $c);
$sides = [$a, $b, $c];
sort($sides);
[$x, $y, $z] = $sides;
if ($x + $y <= $z) {
echo "invalid\n";
} else {
if ($x === $z) {
$kind = "equilateral";
} elseif ($x === $y || $y === $z) {
$kind = "isosceles";
} else {
$kind = "scalene";
}
if ($x * $x + $y * $y === $z * $z) {
$kind .= " right";
}
echo $kind, "\n";
}