DEV Community

kushal Singh Rathore
kushal Singh Rathore

Posted on

Unexpected result while solving given type of expression

Hello friends,

getting zero in the code written below during compilation but the correct answer is 30.

int main()
{
int a=(30/100)*100;
printf("%d",a);
}

here the problem is all the expression is written with int datatype only no float value is here so 0.3 is considered as 0 according to integer and the whole expression is zero.
To correct this we have to provide either upper value as float or denominator as float

and the code is look like this with the correct output.

int main()
{
int a=(30/100.0)*100;
printf("%d",a);
}

Top comments (0)