why is it giving zero?

0 Alam Khattak · January 1, 2015
/images/forum/upload/2015-01-01/542390e8220410e3cc8d69d7cf67d86d.jpg

Post a Reply

Replies

Oldest  Newest  Rating
0 Alam Khattak · January 1, 2015
/images/forum/upload/2015-01-01/ff384bd54454c686ffac51d095949f25.jpg
0 Alam Khattak · January 1, 2015
Krootushas Gesu .now its right ok:)
0 avner turgeman · January 1, 2015
You may also multiply by 100 before division if you want to get an integer result without fraction
i.e:
answer = x * 100 / y
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. 
  • 1

C++

106,985 followers
About

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

Links
Moderators
Bucky Roberts Administrator