why is it giving zero?

0 Alam Khattak · January 1, 2015

Post a Reply


Oldest  Newest  Rating
0 Alam Khattak · January 1, 2015
0 Alam Khattak · January 1, 2015
Krootushas Gesu .now its right ok:)
0 Gaston Seneza · January 2, 2015
The reason you have to convert  X and Y or one of them in decimal ( 120.0 or 150.0) ,  is because your default values ( in this case X and Y) are integers, so every time computer divides x/y, the answer will be 0.8... and the integer here is 0.  the computer will only choose integer number 0 and ignore the rest decimals, because X and Y are both declared as integers in your function definition's parameters, so the result must be an integer. In that case, when we use 120.0 or 150.0, we change nothing to the value, but the compiler will have a reason to give us an exact answer of our division as a "double".  remember that a computer does what awe tell it to do.

I would like to point out that multiplying by 100 before division in this case also works perfectly, but it won't always work for some values. for example when the number of decimals  exceeds two. 
0 avner turgeman · January 1, 2015
You may also multiply by 100 before division if you want to get an integer result without fraction
answer = x * 100 / y
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator