-2

I was doing a data science pet project, this is a simplified version of the problem:

int b = 102;
int c = 248;
double a = (b / c) * 100;
Console.WriteLine(a); // prints 0

This code prints zero for some reason. Any other alternatives? Why is this happening?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
MrBugaboo
  • 3
  • 2
  • 2
    you have *integer division*: `b / c == 0` since both `b` and `c` are of type `int` should be `double a = 100.0 * b / c;` (note `100.0` which is `double`) – Dmitry Bychenko May 11 '20 at 21:06

1 Answers1

1

Because "b / c" is zero, you should use:

int b = 102;
int c = 248;
double a = ((double)b / c) * 100;
int k = Convert.ToInt32(a);
Console.WriteLine(k);