Loan calculator rounding error | Sololearn: Learn to code for FREE!

+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

12/1/2020 11:13:32 PM

Bobby Dixon

5 Answers

New Answer

+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

+4

Use math.ceil() as recommended by challenge. https://www.sololearn.com/discuss/2608182/?ref=app https://www.sololearn.com/discuss/2605192/?ref=app

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.

0

I used the formula, it turned out pretty good https://code.sololearn.com/ctEcLcYseT8O/?ref=app

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); } }