
I solved this problem using cout.precision(4); because when i used cout.precision(2); it didnt output the decimals. Before this i thought that the parameter in precision function is the number of decimal places it will show.
Can anyone explain how this really works?

I don't know how that work but use printf("0.2f",sol). It's faster. ;) ( I think... )

:D i know about that , but what i'm asking is why cout.precision(4) is 2 decimal places precision

Try googling before asking: http://www.cplusplus.com/reference/iostream/ios_base/precision.html

Ah, I forgot to put using namespace std; :)

If you don't use "fixed" it wont print out trailing zeros.
So if you are trying to:
cout << setprecision(4) << sqrt(.16) ;
this would print: .4
However if you do this:
cout << fixed << setprecision(4) << sqrt(.16);
the result would be: .4000