+ 6

Loan calculator rounding error

All variables are integers but it rounds the answer and does not match the output for the test. I do not understand why. With input of 100000 result for test is off by 1 https://code.sololearn.com/c0W9nDJExjMT/?ref=app

1st Dec 2020, 11:13 PM
Bobby Dixon
Bobby Dixon - avatar
5 ответов
+ 2
For this I used Math and rounded my answer after multiplying the remaining amount by 9/10... I hope this helps https://code.sololearn.com/cDlJfxcG3v15/?ref=app
2nd Dec 2020, 1:34 AM
Steven M
Steven M - avatar
2nd Dec 2020, 1:57 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
0
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.
16th Jan 2021, 4:44 AM
Lam Wei Li
Lam Wei Li - avatar
0
I used the formula, it turned out pretty good https://code.sololearn.com/ctEcLcYseT8O/?ref=app
10th Apr 2021, 9:56 PM
GGbet
GGbet - avatar
0
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int loan; //your code goes here for(int i=1;i<=3;i++) { loan =amount/10; amount-=loan; } System.out.println(amount); } }
14th Jun 2021, 9:20 AM
Dharmi Sri
Dharmi Sri - avatar