Need help in outputting the float version of this. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help in outputting the float version of this.

https://code.sololearn.com/cZz20UK2q1qV/?ref=app

2nd Jun 2021, 8:58 PM
Frimpong Godfred
Frimpong Godfred - avatar
4 Answers
+ 5
In the expression "50 * 5 / 100", as the precedence of multiplicatikn and division is the same, "50 * 5" is evaluated first. So the resulting expression is (50 * 5) / 100 = 250 / 100 = 2 (as integer division takes place) Changing any of the 3 numbers to float will fix the issue and give the correct result. 50.0f * 5 /100 50 * 5.0f / 100 50 * 5 / 100.0 50.0f * 5.0f / 100.0 and so on, will all give the correct result
2nd Jun 2021, 9:07 PM
XXX
XXX - avatar
+ 2
Thank you XXX It worked
2nd Jun 2021, 9:10 PM
Frimpong Godfred
Frimpong Godfred - avatar
0
Hi RockStar C++ automaticly rounds. Check this for methods to fix it: https://stackoverflow.com/questions/33854825/stdcout-with-floating-number
2nd Jun 2021, 9:02 PM
Ollie Q
Ollie Q - avatar
0
Ollie Q Olathe stack gave me a lot that I think I have to learn about that because I tried "std::cout << 5 * 5 / 100 << std::endl; but didn't work
2nd Jun 2021, 9:11 PM
Frimpong Godfred
Frimpong Godfred - avatar