C : Discount Question | SoloLearn: Learn to code for FREE!

0

C : Discount Question

A very strange behavior I notice today while solving a pretty simple question... =================== Cost Price and Discount(%) is given, find Selling Price =================== #include <stdio.h> #include <stdlib.h> int main() { int b, d, p; //b is CP, d is Discount Rate, p is SP scanf("%d\n%d", &b, &d); p = b-(b*d/100); // This is where i want to stress printf("%d", b); printf("\n%d", p); return 0; } =================== In the above code, if I change, this line -> "p = b-(b*d/100);" to -> "p = b-(b*(d/100));" the answer goes wrong, completely!!! =================== Really strange, just for a parenthesis!!! - my intention on giving it was just to make C understand it well, but went hard on me. =================== If anyone can help me correct my wrong concept, it would be really appreciated... Thanks

4/10/2021 9:37:40 AM

Arun Bhattacharya

3 Answers

New Answer

+6

d/100 is always 0. Because d is integer, 100 is integer, and d is between 0 to 100. int / int = int. b*d is still integer, but is larger than 100, which produces an integer greater than 0.

+2

Thanks CarrieForle

0