0
Loan Calculator - Java
You take a loan from a friend and need to calculate how much you will owe him after 6 months. You are going to pay him back 10% of the remaining loan amount each month. Let's assume the loan amount is $100,000. Sololearn is expecting an output of $53,144. This is $1 short. For month 1, 10% of $100,000 is $10,000... minus 100000 is 90000. Month 2, 10% of $90,000 is 9000... minus $90,000 is $81,000. So on and so forth. The final balance at the end of month 6 is $53,145. Understanding that all of the data types are integers (2 to 4 bytes), where did the dollar go? Not sure how to include a copy of the code so I will input a link. https://repl.it/@ETII/SoloLearn
3 Réponses
+ 2
https://code.sololearn.com/cp6GXE4Rc84v/?ref=app
The reason why you get 54145 is because of Integer division. It ignores decimals (rounding down). Ultimately when you do a minus, you get an extra 1.
1: 100000 - 100000/10 = 90000
2: 90000 - 90000/10 = 81000
3: 81000 - 81000/10 = 72900
4: 72900 - 72900/10 = 65610
5: 65610 - 65610/10 = 59049
6: 59049 - 59049/10
= 59049 - 5904 (missing precision)
= 54145 (extra 1, expected 54144)
To bypass, you can do "amount = amount * 90 / 100".
Do not hard-code -1 or +1.
One good example will be 0. After 6 months, it will still be 0. By hard-coding -1, it results in -1 which is wrong.
+ 1
Solo Learn is expecting $53,144. The final balance that I get when I run my program is $53,145. Should it round up or not round at all and just drop everything after the decimal? All my number data types are integers.
0
The payment should round up if it's not divisible by 10.
Also you only need to output the "final" balance. You don't need to output the process.