-2

perc=(sum/total)*100;

i've been trying to put this in the code in cprogramming,but output for this part is showing 0,,,Why is this happening and what else should I do in these types of scenerios?

  • Please provide a [minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). It should be obvious - the type and value of `sum` and `total` matter and we can't explain the result without knowing those. – kaylum Feb 28 '20 at 03:16
  • you can infer sum < total, and that integer division is causing this to be 0, which then 0*100 assigns 0 to perc. – Bwebb Feb 28 '20 at 03:17
  • Welcome @Intishar, please provide minimal working code and elaborate your question properly, so that it is easy to understand and guide you accordingly – user1584253 Feb 28 '20 at 04:14

1 Answers1

0

Since you didnt post source and this question will be closed soon, the cause is from integer division most likely. Try this instead.

float perc = ((float)sum / (float)total) * 100.0;

Heres a post on integer division if you are interested in learning why this behavior is doing what it did: What is the behavior of integer division?

Bwebb
  • 675
  • 4
  • 14
  • @IntisharRahman you can use (double) too if you want to hold larger data but 0-100 should be fine with float. Accept this answer as a solution to your question if it solved your problem (hit the green checkmark) – Bwebb Feb 28 '20 at 03:47