C : Discount Question | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 2

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

10th Apr 2021, 9:37 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
6 Respostas
+ 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.
10th Apr 2021, 9:59 AM
šĹ†Áü•ťĀŹŚČáԾƜąĎšĻüśėĮ
šĹ†Áü•ťĀŹŚČáԾƜąĎšĻüśėĮ - avatar
+ 5
Thanks CarrieForle
10th Apr 2021, 10:03 AM
Arun Bhattacharya
Arun Bhattacharya - avatar
+ 2
You can also use scanf("%f%f) instead of using escape sequence in between.
24th May 2021, 7:21 AM
Gourab Mukherjee
Gourab Mukherjee - avatar
+ 1
The remainders (or modulo) are dropped from integer math in C. You can change your variable types to float or cast for the math. However I would use the float type variables and if you are looking for whole numbers utilize the rounding functions.
11th Sep 2021, 9:02 PM
William Owens
William Owens - avatar
0
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.
25th Nov 2022, 5:52 AM
Abdulaziz Mirabdulllayev
Abdulaziz Mirabdulllayev - avatar
0
I start a c language ūüėä
26th Jul 2023, 5:43 AM
Gaurav Lokhande
Gaurav Lokhande - avatar