0
b=((a/n)*100);

This line of code always gives me 0 regardless of the value of "a" and "n". I don't think this depends on anything else in the code, since everything else is working fine. This is probably very stupid, but can anyone help?

Cu be
  • 1
  • 7
    my crystal ball says you're doing integer division and `n > a` – yano Oct 15 '21 at 14:55
  • 1
    You haven't shown us enough to know what the problem might be. How are the variables declared? How are they assigned values? How do you print the result? – dbush Oct 15 '21 at 14:55
  • @Cu be It is unable always to produce 0. For example consider a equal to 1 and n equal to 1.:) – Vlad from Moscow Oct 15 '21 at 14:55
  • If `a` and `n` are both 1, that gives me 100. What values did you actually test this with? – Joseph Sible-Reinstate Monica Oct 15 '21 at 14:55
  • 1
    " don't think this depends on anything else in the code" Yes it does, namely the variable declarations of all variables involved. Without it, nobody can answer the question, just speculate. – Lundin Oct 15 '21 at 14:57
  • Thanks, yano is right, I'm doing an integer division with n>a – Cu be Oct 15 '21 at 15:06
  • @Cube to elaborate, in integer division, the fractional part is discarded and only the whole number quotient is retained. So even 999/1000 is 0 with integer division. – yano Oct 15 '21 at 15:18
  • `double b=( ( ((double)a)/n)*100);` is probably what you need. – Shark Oct 15 '21 at 15:49

0 Answers0