#include<stdio.h>
#include<math.h>
int main()
{
double a,b,c,A,p,
scanf("%lf %lf %lf",&a,&b,&c); //Output is put in integers
p = (a+b+c)/2;
A = sqrt(p*(p-a)*(p-b)*(p-c));
printf("Area of triangle is %lf",A);
//The output is coming out to be -nan for some inputs.
return 0;
}
Asked
Active
Viewed 133 times
-1
-
2Input values? BTW - always check the value returned by `scanf`, i.e. does it return 3 in your case? – Support Ukraine Aug 30 '17 at 09:39
-
3What is your input? What is your expected output? What is your actual output? ***Copy-paste as text*** into the question body. Also please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). I also recommend you take some time to read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert, and learn how to use a debugger. – Some programmer dude Aug 30 '17 at 09:41
-
1A look at the the documentation of [`sqrt`](http://en.cppreference.com/w/c/numeric/math/sqrt) reveals that the function returns `nan` (not a number) in cases where the square root could not be calculated, i.e. negative numbers. So Your input values probably produce a negative number which you are trying to calculate the square root of. Though I can only guess, since you have not provided a [mcve] including your users input. – muXXmit2X Aug 30 '17 at 09:48
-
Will not compile. Always copy/paste the code you actually tested. – Martin James Aug 30 '17 at 10:03
-
Which compiler did you use for testing? It should not generate a testable executable with the missing ';' typo:( – Martin James Aug 30 '17 at 10:06
2 Answers
3
For the input
1.0 2.0 5.0
you get
p = 4.0
p - a = 3.0
p - b = 2.0
p - c = -1.0 // notice the sign
so you end up with
sqrt(-24.0) // ups... sqrt of a negative number
Consequently you get -nan
Maybe you should use fabs
to get rid of negative values.
BTW: nan
means "not a number" - see https://en.wikipedia.org/wiki/NaN
BTW: Always check the value returned by scanf
to make sure it actually scanned the expected number of values, i.e.
if (scanf("%lf %lf %lf",&a,&b,&c) != 3)
{
// Input failure - add error handling...
// For instance:
printf("Illegal input - please provide 3 double as input\n");
return -1;
}

Support Ukraine
- 42,271
- 4
- 38
- 63
-
Or maybe the OP wants to use complex numbers: [`csqrt()`](http://en.cppreference.com/w/c/numeric/complex/csqrt): https://stackoverflow.com/a/9860772/8051589. But I don't think so, however nice answer! +1 – Andre Kampling Aug 30 '17 at 09:48
-
The 'fabs' is not working and is generating the same answer(nan) with the negative answer. – Barry Aug 30 '17 at 17:10
-
@Barry - Sounds like you are using `fabs` incorrectly. Try `A = sqrt(fabs(p*(p-a)*(p-b)*(p-c)));` – Support Ukraine Aug 30 '17 at 17:18
0
double a,b,c,A,p;
your forgot the ; ?
And if you want to printf a float or double you use %f not %lf
printf("Area of triangle is %f",A);
You use %lf only for an input for a float or double, for output you use %f
The other thing could be that the sqrt() is negative.

sebastian15038
- 67
- 5
-
2For [`printf`](http://en.cppreference.com/w/c/io/fprintf) the formats `"%lf"` and `"%f"` does the same thing. – Some programmer dude Aug 30 '17 at 09:43