# why is it giving zero?

 Alam Khattak · January 1, 2015

## Replies

 Crisa Gazzola · January 2, 2015 Thanks for the explanation, I understand now! 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. Crisa Gazzola · January 2, 2015 Why do you have to convert both x and y to doubles before you divide in order to not get 0? Can someone explain this to me? Thank you. avner turgeman · January 1, 2015 You may also multiply by 100 before division if you want to get an integer result without fractioni.e:answer = x * 100 / y Alam Khattak · January 1, 2015 Krootushas Gesu .now its right ok Alam Khattak · January 1, 2015
• 1

## C++

104,882 followers