Interview Question
Software Engineer / DevelopersBetter way is to try to find the area of the triangle using Hero's formula. Let a,b,c be the three sides of the triangle.
s = (a+b+c)/2;
if(s*(s-a)*(s-b)*(s-c) <= 0){
"Error"
}
else if((a==b)||(b==c)||(c==a)){
"Isoceles"
}
else if(a==b && b==c){
"equilateral"
}
else if((a != b) && (b != c) && (c != a)){ //or just else, no need of comparison
"Scalene"
}
Is it a school homework?
- Ankush Bindlish December 12, 2009Triangles can be classified according to the relative lengths of their sides:
* In an equilateral triangle, all sides are of equal length. An equilateral triangle is also an equiangular polygon, i.e. all its internal angles are equal—namely, 60°; it is a regular polygon.[1]
* In an isosceles triangle, two sides are of equal length (originally and conventionally limited to exactly two).[2] An isosceles triangle also has two equal angles: the angles opposite the two equal sides.
* In a scalene triangle, all sides have different lengths. The internal angles in a scalene triangle are all different.[3]
Image removed by sender. Equilateral Triangle
Image removed by sender. Isosceles triangle
Image removed by sender. Scalene triangle
Equilateral
Isosceles
Scalene
Triangles can also be classified according to their internal angles, described below using degrees of arc:
* A right triangle (or right-angled triangle, formerly called a rectangled triangle) has one 90° internal angle (a right angle). The side opposite to the right angle is the hypotenuse; it is the longest side in the right triangle. The other two sides are the legs or catheti (singular: cathetus) of the triangle.
* An oblique triangle has no internal angle equal to 90°.
* An obtuse triangle is an oblique triangle with one internal angle larger than 90° (an obtuse angle).
* An acute triangle is an oblique triangle with internal angles all smaller than 90° (three acute angles). An equilateral triangle is an acute triangle, but not all acute triangles are equilateral triangles.
Image removed by sender. Right triangle
Image removed by sender. Obtuse triangle
Image removed by sender. Acute triangle
Right
Obtuse
Acute
Image removed by sender. \underbrace{\qquad \qquad \qquad \qquad \qquad \qquad}_{}
We can find CosA, CosB , CosC using the cosine formula.
Image removed by sender. \cos C=\frac{a^2+b^2-c^2}{2ab}\,.
If ( a+b <= c || b+c <=a || c+a <= b)
Print Triangle cannot be formed.
cosA = <apply formula>
cosB = <apply formula>
cosC = <apply formula>
Print a==b ? (b==c ? "Equilateral":"Isosceles") : (b==c?"Isosceles" : "Scalene";
If ( 0 == CosA || 0 == CosB || 0 == CosC)
Print Right Angled
else If ( 0 != CosA && 0 != CosB && 0 != CosC)
Print Oblique Angled
else If ( 0 > CosA || 0 > CosB || 0 > CosC)
Print Obtuse Angled
else If ( 0 < CosA && 0 < CosB && 0 < CosC)
Print Acute Angle
else
Print "Triangle cannot be formed."