0

Loan calculator problem answer differ from few values

import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int value; //your code goes here for(int x=0;x<6;x++){ value=(amount/100)*10; amount-=value; } System.out.println(amount); } } My output:- 53150 Expected output:-53144

5th Jan 2021, 9:38 AM
Harsh Sharma
Harsh Sharma - avatar
4 ответов
+ 4
Check my answer here.. I've explained why the test cases didn't pass. Hope it helps! https://www.sololearn.com/Discuss/2645314/?ref=app
5th Jan 2021, 9:47 AM
Minho
Minho - avatar
+ 6
You're welcome 😊
5th Jan 2021, 12:41 PM
Minho
Minho - avatar
+ 3
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.
28th Jan 2021, 1:15 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
Thank you😊😊for your help
5th Jan 2021, 12:39 PM
Harsh Sharma
Harsh Sharma - avatar