Why the output for x is 0 ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
19th May 2020, 7:38 PM
Intan Suraya
Intan Suraya - avatar
2 Answers
+ 3
Use float or double if you need precision. Int contains whole numbers, so in division the precision is "cut off".
19th May 2020, 7:46 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 3
What you should do to avoid it are two things: 1. Declare x as float or double 2. Write the division expression with at least one of the arguments as a floating point number, like this: https://code.sololearn.com/clZkvxVW7Hl7/?ref=app Also, feel free to refresh the lesson on the division operator: https://www.sololearn.com/learn/CPlusPlus/1609/
19th May 2020, 7:49 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar