-1
printf("The Angle A Is: %f", (acos((b*b + c*c - a*a)/2*b*c)*180/3.14159265359));

This is a simple program which returns the angle A if the sides a, b, c are entered.

I have checked whether the triangle is possible or not.

if((a+b) <= c || (b+c) <= a || (c+a) <= b)
    return 0;

I am getting correct output for some inputs. For 1 1 1 I get 60.

But for the input 7 7 7 I am getting -1.#IND00 instead of the 60 I should be getting.

Now I came to know by reading another answer that it stands for indeterminate values (specifically a non-zero number divided by zero) but here that is not the case.

So why am I getting this error?

Here is the full program:

int main()
{
    float a, b, c;

    printf("Enter The Sides of The Triangle(a, b, c): ");
    scanf("%f %f %f", &a, &b, &c);

    if((a+b) <= c || (b+c) <= a || (c+a) <= b)
    {
        printf("Triangle Not Possible.");
        return 0;
    }

    printf("The Angle A Is: %f", (acos((b*b + c*c - a*a)/2*b*c)*180/3.14159265359));
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
Harshit Joshi
  • 260
  • 4
  • 15
  • do not tag both C and C++. They're very different languages. Also learn [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/comment-page-1/) – phuclv Sep 07 '19 at 07:36
  • @phuclv Sorry. I will remember that next time. – Harshit Joshi Sep 07 '19 at 07:44

1 Answers1

2

You're missing some parentheses. Instead of writing acos((b*b + c*c - a*a)/2*b*c),
write acos((b*b + c*c - a*a)/(2*b*c)). Otherwise, b*c ends up in the numerator of your fraction, not the denominator, and you try to take the arccos of an impossible number.

Generally speaking, when you have a problem like this, it helps to simplify and break down the code. If acos returns an unexpected value, then it must have an incorrect argument. So calculate the argument to acos, put it in a variable, print that, make sure it's correct, and then move on.

hobbs
  • 223,387
  • 19
  • 210
  • 288