Posted By

jaircazarin on 10/10/08


Tagged

Algorithms Microsoft-Interview Geometry


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

jaircazarin
wizard04
skammer


Type of Triangle


 / Published in: C
 

Given a, b, c find which kind of triangle can be build with those values.

  1. #include<stdio.h>
  2. #define NONE -1
  3. #define EQUILATERAL 0
  4. #define ISOSCELES 1
  5. #define SCALENE 3
  6.  
  7. int isTriangle(int a, int b, int c)
  8. {
  9. if(a < 1 || b < 1 || c < 1)
  10. return NONE;
  11. if(a+b <= c || a+c <= b || c+b <= a)
  12. return NONE;
  13. if(a == b && a == c)
  14. return EQUILATERAL;
  15. if(a == b || b == c || a == c)
  16. return ISOSCELES;
  17. else
  18. return SCALENE;
  19. }
  20.  
  21. int main()
  22. {
  23. printf("%d\n", isTriangle(11,10,17));
  24. printf("%d\n", isTriangle(11,10,11));
  25. printf("%d\n", isTriangle(11,11,11));
  26. printf("%d\n", isTriangle(11,110,11));
  27. printf("%d\n", isTriangle(11,0,11));
  28. return 0;
  29. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: wizard04 on October 10, 2008

In the second if statement, it should be `

Posted By: wizard04 on October 10, 2008

(Sure wish they'd implement Markdown correctly...)

In the second if statement, it should be <=. E.g., if a=1, b=1, and c=2, it can't be a triangle.

You need to login to post a comment.