You have integer division.
This line is ok (just)
profit=sales*10/100;
But this causes a problem:
|
sales =sales- 4/100*sales;
| |
When evaluating the expression
4/100*sales
, it proceeds from left to right. First divide 4 by 100, giving zero. (remember, those are integers). Next, multiply 0 by sales again giving zero.
It's best, unless you specifically want integers (it is ok for the loop counter
year
), use a decimal point to make the numbers a floating-point type.
|
sales = sales - 4.0/100.0 * sales;
| |
Also, you need to calculate the profit inside the loop too, for each year.
Let's go back and look at this line again
sales*10/100
. Because the calculation proceeds from left to right,
sales*10
is done first. Sales is type
float
, so the result is
float
too. The same with the divide by 100 here. But it is less dangerous to not depend on the order of operations, use
sales*10.0/100.0
or more simply
sales*0.1
.