-2
void main()
{
    int a = 5;
    int b = 10;
    int c;
    c = (a / b) * 100;

    //5/2=0.5
    //0.5*100=50

    printf("C value is : %d", c);
}

Why the output is Zero when the output should be 50. This is a question which I have been not caring for a long time. I wanna know what's happening behind that expression.

  • 3
    5/10 is 0, because the values are ints, and integer division rounds down. – Paul Hankin Jan 26 '22 at 13:46
  • 1
    In integer division, x/y will always be zero if x has lower magnitude than y. – Adrian Mole Jan 26 '22 at 13:47
  • 1
    You can replace with `(a * 100) / b`. – kiner_shah Jan 26 '22 at 13:48
  • 1
    Many answers given recommending floating point, only one mentioning, though, the rounding issues :( Floating point math in general is not exact, even such a simple value like 0.1 is periodic in binary and needs to be rounded to fit into a float or double. General recommendation: If you can avoid FP with reasonable effort, then do so. In your case just swapping operations, as @kiner_shah recommended, would be a potential solution *provided* you do not have to consider overflow (i.e. `a` getting to large yet to be multiplied with 100). – Aconcagua Jan 26 '22 at 14:07

4 Answers4

3

The expression (a / b) will evaluate to 0, because it is an integer division, not a floating-point division. Multiplying 0 with 100 will yield 0.

If you want to use floating-point numbers, you should change your program to the following:

int main( void )
{
    double a = 5.0;
    double b = 10.0;
    double c;
    c = (a / b) * 100.0;

    printf( "C value is : %lf\n", c );
}

In order to enforce that floating-point division is used instead of integer division, it is actually sufficient to make one of the operands floating-point. This program will also work:

int main( void )
{
    int a = 5;
    int b = 10;
    int c;
    c = ( (double)a / b) * 100;

    printf( "C value is : %d\n", c );
}

However, you should generally be careful of floating-point inaccuracy, which could cause a number to be rounded down, when converting it to an int. In this particular case, this is not an issue, because 0.5 can be represented exactly in IEEE 754 floating-point format. But with other numbers, you will likely encounter issues, as described in the linked question.

In your case, it is actually possible to solve the problem without using floating-point numbers, by performing the multiplication with 100 before performing the division, by changing the line

c = (a / b) * 100;

to:

c = a * 100 / b;

That way, it will first calculate a * 100, which is 500, and then divide that by 10.

However, this will only work in cases in which

  • the division does not have any remainder, and
  • the values of the sub-expressions are not too high that they are not representable as an int.

In other cases, it is probably better to use floating-point numbers.

However, it is possible to extend the range of an int by using long int or long long int instead.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
1

The expression a / b has integers for both operands, so integer division is performed which truncates any fractional portion of the result.

You'll need to cast one of the operands to a floating point type so that floating point division is carried out.

c = ((double)a / b) * 100;

Also, while in this particular case you should get the exact result you want, floating point arithmetic is not exact and rounding errors may occur.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

Because the int value of 5 / 10 is 0 (0.5 gets rounded down). You need to use doubles or floats for this

void main()
{
    float a = 5;
    float b = 10;
    float c;
    c = (a / b) * 100;

    //5/2=0.5
    //0.5*100=50

    printf("C value is : %f", c);
}

outputs 50.0

Filipe S.
  • 74
  • 5
0

following the comments above, float should be the best answer.

void main()
{
    int a = 5;
    int b = 10;
    int c;
    c = a * 100 / b;

    printf("C value is : %d", c);
}
tiago calado
  • 156
  • 7
  • 1
    The text of your answer does not correspond to the code of your solution. In the text of your answer, you state that `float` is the "best answer", but your solution does not use `float` or `double`, but instead provides an integer solution. – Andreas Wenzel Jan 26 '22 at 14:19