0

Getting the output in Loan calculator as one number higher than expected. So, if the answer is 54321, my output is 54323.

The output needs to be the balance amount after 10% of the amount is paid each month after six months. Tried using rounding function and typecasting, but same result. Can you please let me know if something is wrong with the code (pasting as text only). Thank you. 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 x,z; double y; for (x=0;x<6;x++) { y = amount / 10; z = (int) Math.round (y); amount = amount - z; if (x == 5) {System.out.println (amount);} } }

23rd Feb 2021, 4:55 AM
Khagesh Deshpande
Khagesh Deshpande - avatar
10 Réponses
+ 5
Please don't paste as plain text. Instead save it in a private code in the code playground and link it here. It makes it easier to reference line numbers and compiler errors/warnings first of all don't need the line "z = (int) Math......." So you can remove that line and change `amount - z` on line 15 to `amount - y` Secondly, you're right in keeping the type of y as double, but it's of no use as `amount / 10` will always yield an integer. So there is no point in assigning an int to a double variable. Changing 10 to 10.0 will fix the problem. Now, there will be an error saying 'possible lossy conversion from......' on line 15 `amount = amount - y;` To fix the error, simply cast the result of `amount - y` to int before assigning it to `amount`, like so `amount = (int) (amount - y);`
23rd Feb 2021, 5:19 AM
XXX
XXX - avatar
+ 3
Khagesh Deshpande I never told you have an integer instead of double. I'm saying, change 10 to 10.0 so that the result is actually a double. Then there will be an error in the line `amount = amount - y` because `amount - y` yields a double while `amount` is an int. So fix that error, you need to cast the result of `amount - y` to int before assigning it. Also, I forgot to say this in my previous answer, there is no point in having the if-condtion inside the for-loop. Remove it, and instead output the value of `amount` after the loop ends. You also need another '}' at the end. This is how it will work, when amount = 1000 y = amount / 10.0 1) y = 100.0 amount = (int) (1000 - 100.0) = 900 2) y = 90.0 amount = (int) (900 - 90.0) = 810 3) y = 81.0 amount = (int) (810 - 81.0) = 729 4) y = 72.9 amount = (int) (729 - 72.9) = (int) 656.1 = 656 5) y = 65.6 amount = (int) (656 - 65.6) = (int) 590.4 = 590 6) y = 59.0 amount = (int) (590 - 59.0) = 531
23rd Feb 2021, 5:51 AM
XXX
XXX - avatar
+ 3
No I meant to temporarily change the input to a double after its already been entered. Could you not do something like: double y = amount; for (...) { y *= 0.9; } System.out.print((int) y);
23rd Feb 2021, 5:53 AM
Elijah Brown
Elijah Brown - avatar
+ 1
Do you pass the tests if you replace the 3 math/variable lines in the for loop with: amount *= 0.9; If that doesn't work then you need to store the 'amount' variable as a double while it passes through the for loop because you lose accuracy every time it is cast to an integer.
23rd Feb 2021, 5:20 AM
Elijah Brown
Elijah Brown - avatar
+ 1
Thank you guys. It worked.
23rd Feb 2021, 6:00 AM
Khagesh Deshpande
Khagesh Deshpande - avatar
+ 1
Ipang thanks for pointing it out
23rd Feb 2021, 6:33 AM
Khagesh Deshpande
Khagesh Deshpande - avatar
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); int months = 6; for (int i = 0; i < months; i++) amount = amount * 90 / 100; System.out.println(amount); } }
23rd Feb 2021, 10:03 AM
vatsal k
vatsal k - avatar
0
What purpose does the for loop serve? I assume the challenge is to print the input with a 10% discount applied? Could you not just: System.out.print(amount * 0.9);
23rd Feb 2021, 5:13 AM
Elijah Brown
Elijah Brown - avatar
0
Need the output to be the balance amount from the loan after 10% is paid is each month for six months. Sorry, should have mentioned it in the statement.
23rd Feb 2021, 5:16 AM
Khagesh Deshpande
Khagesh Deshpande - avatar
0
Elijah Brown the problem statement needed amount to be integer, can't change it. XXX as amount changes in each loop as amount - z, amount can be any integer. Example: Amount =1000 1st month- z = amount/10 = 100. New amount = 900. 2nd month - z = 90. New amount = 810. 3rd month = z = 81. New amount = 729. 4th month - z = 72.9 That is why I need a double instead of integer.
23rd Feb 2021, 5:32 AM
Khagesh Deshpande
Khagesh Deshpande - avatar