- 1

in C++; I wanted to take a simple discount (part of my program) , i wrote the program, and my formula is correct but output is w

int main() { float d; d =50 - ( 50/100*15); cout << d; } output is 50; it should be 42.5

6th Jul 2021, 5:11 PM
faizan moriani
faizan moriani - avatar
3 Antworten
+ 3
(50.0/100.0*15.0)
6th Jul 2021, 5:15 PM
visph
visph - avatar
+ 5
faizan moriani , the reason for your problem is that the numbers in the expression (50 / 100 * 15) are all integer numbers. so 50 / 100 => 0 and 0 * 15 => 0, so finally 50 - 0 => 50.
6th Jul 2021, 5:22 PM
Lothar
Lothar - avatar
+ 2
by using only integer literals you get the result as integer (0*15 == 0)
6th Jul 2021, 5:17 PM
visph
visph - avatar