### need some help please

Hello,
QUESTION: why price input as 10000 then the value of payment is 0?

#include <iostream>
using namespace std;

int main()
{
float price, payment;

cout << "\n PAYMENT OF A CAR AFTER 5% DISCOUNT:";
cout << "\n -----------------------------------";

cout << "\n Enter the car price: ";
cin >> price;
payment = 95/100*price;

cout << "\n OUTPUT: "<< payment;
return 0;
}
95/100 (left-to-right associativity) does integer division. The result is 0, because it rounds down to the nearest integer. And then 0 times anything is 0.

To prevent this, make at least one of the operands a floating-point type.

ex: `payment = 95.0/100*price;`

Here is an SO link that explains it in more detail: https://stackoverflow.com/questions/3602827/what-is-the-behavior-of-integer-division

Last edited on
It's what we call the order of precedence of operators.
https://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx

Consider `payment = 95/100*payment;`

95/100 is executed first. As they are integers, the value is zero. Then that zero is used to multiply `payment`.

You can force the correct behaviour by using double constants.
`payment = 95.0/100.0*payment;`
i thought if we already declared price and payment as float the output will be in float (correct)? what is the explanation for this? tqvm.
95/100*price is parsed as (95/100) * price
(multiplication and division have equal precedence, they associate from left-to-right.)

Evaluated "as if":
 ``12345678`` ``````const int a = (95/100) ; // both operands are of type int, integer division, result is 0 const float b = float(a) ; // convert the result of the integer division to float, result is 0.0f // see: usual arithmetic conversions http://en.cppreference.com/w/c/language/conversion const float c = b * price ; // this is the result of the value computation of 95/100*price payment = c ; // assign the result to payment ``````

Note: strongly favour using double as the default floating point type.
Registered users can post here. Sign in or register to post.