Ive written the program below to determine types of triangle from their side lengths. The problem is that it cannot seem to determine when its a right angled triangle. It gives either acute (all angles less than 90 degrees) or obtuse (one angle greater than 90 degrees).
The side lengths 3, 4 and 5 will give one interior angle of exactly 90 degrees, yet the program produces 89.99999 etc. Im sure its a rounding problem, but can anyone help me rewrite the program to cure it?
Code:#include <stdio.h>
#include <math.h>
int main()
{
char another='Y';
double a,b,c,A,B,C;
printf("This is a program to categorise triangles by side length and angle size\n");
while(another == 'Y' || another == 'y')
{
printf("Enter 3 real numbers for each side of the triangle\n");
scanf("%lf%lf%lf",&a,&b,&c);
if(a>0 && b>0 && c>0)
{
A = (b*b + c*c - a*a)/(2*b*c); /* calculates the value of cosA */
B = (a*a + c*c - b*b)/(2*a*c);
C = (a*a + b*b - c*c)/(2*a*b);
/* checks triangles are possible */
if((A>-1 && A<1) && (B>-1 && B<1) && (C>-1 && C<1))
{
printf("The triangle you have input has side lengths %lf, %lf, %lf\n\n",a,b,c);
printf("Angle A = %lf radians",acos(A));
printf(" or %lf degrees\n",acos(A)*(180/3.1415927));
printf("Angle B = %lf radians",acos(B));
printf(" or %lf degrees\n",acos(B)*(180/3.1415927));
printf("Angle C = %lf radians",acos(C));
printf(" or %lf degrees\n",acos(C)*(180/3.1415927));
if(a==b && b==c && c==a)printf("\nTriangle is equilateral");
if(a!=b && b!=c && c!=a)printf("\nTriangle is scalene");
if((a==b && c!=a) || (a==c && a!=b) || (b==c && b!=a))printf("\nTriangle is isoceles");
if(acos(A)==acos(0)||acos(B)==acos(0)||acos(C)==acos(0))printf(" and right-angled\n\n");
if(acos(A)>acos(0)||acos(B)>acos(0)||acos(C)>acos(0))printf(" and obtuse\n\n");
else if(acos(A)<acos(0)&&acos(B)<acos(0)&&acos(C)<acos(0))printf(" and acute\n\n");
}
else printf("Triangle is impossible!\n\n");
}
else printf("Input is invalid, lengths must be real numbers\n\n");
printf("Would you like to categorise another triangle?(Y or N)");
another=getchar();
while (another !='Y'&&another !='y'&&another !='n'&&another !='N')
another=getchar();
}
printf("\nThis program is now ending\n\n");
return 0;
}