0

I am unable to where the error in this is.

There's this problem: 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. Create a program that takes the loan amount as input, calculates and outputs the remaining amount after 6 months. Sample Input: 20000 Sample Output: 10628 Here is the monthly payment schedule: Month 1 Payment: 10% of 20000 = 2000 Remaining amount: 18000 Month 2 Payment: 10% of 18000 = 1800 Remaining amount: 16200 Month 3: Payment: 10% of 16200 = 1620 Remaining amount: 14580 Month 4: Payment: 10% of 14580 = 1458 Remaining amount: 13122 Month 5: Payment: 10% of 13122 = 1313 Remaining amount: 11809 Month 6: Payment: 10% of 11809 = 1181 Remaining amount: 10628. I provided this solution: import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here int m, payment; for(m = 0; m < 6; m++){ payment = (amount / 10); amount = amount - payment; } System.out.println(amount); } } My outputs are always 1 more than the expected out put but I can't figure out where the error is. Please help.

29th Dec 2020, 12:01 PM
Tommy Mensah
5 odpowiedzi
+ 6
Tommy Mensah Your code will also work if you change the statement, payment = (amount/10); into payment = (int) Math.ceil (amount * 0.1); If you look closely at the example in Month 5: Payment: 10% of 13122 = 1313 It's returning the smallest value that is greater than or equal to the argument. So for rounding up purposes Java Math class has a method named ceil() which looks like this, public static double ceil(double parameter)
29th Dec 2020, 12:48 PM
Minho
Minho - avatar
+ 3
Tommy Mensah Your statements inside the loop or say logic is what is wrong here . Try this : for( m=0;m<6;++m) { amount=amount*9/10; }
29th Dec 2020, 12:05 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 2
Tommy Mensah https://code.sololearn.com/cp6GXE4Rc84v/?ref=app The reason why you get 10629 is because of Integer division. It ignores decimals (rounding down). Ultimately when you do a minus, you get an extra 1. 1: 20000 - 20000/10 = 18000 2: 18000 - 18000/10 = 16200 3: 16200 - 16200/10 = 14580 4: 14580 - 14580/10 = 13122 5: 13122 - 13122/10 = 13122 - 1312 (missing precision) = 11810 (extra 1, expected 11809) 6: 11810 - 11810/10 = 10629 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
Alphin K Sajan Thanks Alphin, your solution worked. I'd appreciate some clarification on where my logic differed from yours. I still am unable to understand why introducing the payment amount in the calculation threw a wrong answer.
29th Dec 2020, 12:33 PM
Tommy Mensah
+ 1
Tommy Mensah Hope you got the explanation from Minho 🇰🇷 .
29th Dec 2020, 1:59 PM
Alphin K Sajan
Alphin K Sajan - avatar