0
For some reason the output is a value larger than it should print as
This code wants me to print a certain owed loan value from the Loan Value module, but it print the value+1 instead if the actual value... 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 owed=0; for(int i=1;i<=6;i++) { int percentage=amount/10; owed=amount-percentage; amount=amount-percentage; } System.out.println(owed); } }
4 Réponses
+ 1
Name it wont work since i is only used for the month loop not the values
+ 1
Serana Zentha
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.
0
Can you please show input formation... I mean example? Little more details.
0
Name
Here is the monthly payment schedule:
If theoretically 20000 is our starting input we should by month 6 get the stated remaining value, but it prints that value+1. Instead of 10628 it prints 10629
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