Given three sides of a triangle. Write the Program to
determine whether the triangle is :
1) Invalid
2) Right Angled
3) Isoscales
4) Equilateral
5) Not Special
An Isoscales right angled triangle should be taken as a
Right Angled Triangle
Answers were Sorted based on User's Feedback
Answer / sunil
if a, b and c are the three sides of a triangle, then a + b > c
if this is not satisfied, then its not a valid triangle.
To check for right angle, use Pythagoras theorem. Assume
that the longest side is the hypotenuse.
Issosless and Equilateral can be found by simply comparing
the sides.
Is This Answer Correct ? | 65 Yes | 31 No |
Answer / giri
One should check for valid sides also. Side values dhould
be greated than ZERO.
Here is the correct routine:
public static String checkTriangle(int[]
triangleSide){
boolean validTriangle = false;
boolean validSides = true;
String result = "NOT VALID TRIANGLE";
for(int side: triangleSide)
if(side <= 0)
validSides = false;
if(validSides){
for(int count= 0; count< 3 ;
count++){
if(((triangleSide[count%3]
+ triangleSide[(count+1)%3]) > triangleSide[(count+2)%3]))
validTriangle =
true;
}
if(validTriangle){
if( triangleSide[0] ==
triangleSide[1] && triangleSide[2] == triangleSide[1])
result
= "EQUILATERAL";
else{
for(int count= 0;
count< 3 ; count++){
if( (
triangleSide[count%3] * triangleSide[count%3] + triangleSide
[(count+1)%3] * triangleSide[(count+1)%3]) == (triangleSide
[(count+2)%3] * triangleSide[(count+2)%3])){
result = "RIGHANGLED";
break;
}else
if
((triangleSide[count%3] == triangleSide[(count+1)%3]))
result = "ISOSCALAUS";
}
}
if("NOT VALID
TRIANGLE".equals(result))
result = "NOT
SPECIAL";
}
}
System.out.println(result);
return result;
}
Is This Answer Correct ? | 32 Yes | 29 No |
Answer / ganesh bhat
I believe, this is more efficient one. solves all the cases.. comments please. Written in java
public static Map validateTriange(int a,int b,int c)
{
Map props = new HashMap();
boolean isValidTriangle;
int bigSide = a;
if(bigSide<b){bigSide = b;}
if(bigSide<c){bigSide = c;}
boolean isSpecial = false;
if((a+b+c-bigSide)>bigSide)
{
props.put("VALID","YES");
}
else
{
props.put("VALID","NO");
return props;
}
if(a == b||b==c||c == a)
{
props.put("ISOSCELES","YES");
isSpecial = true;
}
if(a == b && b == c)
{
props.put("EQUALATERAL","YES");
isSpecial = true;
}
if(((a*a+b*b+c*c)-bigSide*bigSide) == bigSide*bigSide)
{
props.put("RIGHT_ANGLED","YES");
isSpecial = true;
}
return props;
}
Is This Answer Correct ? | 22 Yes | 22 No |
1.What is difference between symget and & in sas? 2.what is difference between callsymput and %let?
what are the things i had to say in personal introduction in hr round mail me to prasanna.1867@rediff.com
How to call a C++ function which is compiled with C++ compiler in C code?
Is there any standard procedure to test the application as a whole? Or How can I test complete application right from the requirement gathering?
In staad pro, how can we design ROOF SLAB?
pleasew define carrier scope in abap (sap).
Given: coordinates of rectangle-> left bottom and right top points. the rectangles create a hole.Find the maximum area of the hole. eg. 4 rectangles create a hole in between. find its area.
Suppose server object is not loaded into the memory, and the client request for it , what will happen?
Why we need new operator in java at the time of object declaration and why not in c++?
How to swap two String values without using a third variable?
There are 2 tables: EMP : EmpId, Ename, Sal, DeptId DEPT : DeptId, Dname Write a query to find out emp names and their department names. if any emp has null in Deptid the it shows ?No Department?. Write a query to find out those department names which has no employee. Write a query to find out those employees whose salary is greater than their department?s average salary.
Hi guys... I have one doubt ..Exception is a runtime error then why we have checked exception... Thanks in advance