Struggling with Loan problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Struggling with Loan problem

Hi there. I´m struggling with coding the Loan Problem at the end of Module 2 Java. The problem: ----------------------- Loan Calculator 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 -------------------------------------- My code: https://code.sololearn.com/cA172a19a181/# However, I get a 'No output' . The question is why I´m not getting any output. Is there anything wrong in the FOR loop? Thanks in advance

26th Jan 2021, 9:37 AM
Enrique Guia
Enrique Guia - avatar
9 Answers
+ 1
There are some illegal character errors ,I removed the spaces from for loop and it outputs 10629 for 20000
26th Jan 2021, 9:56 AM
Abhay
Abhay - avatar
+ 1
Thanks so much for the answer Abhay. I removed the spaces from the FOR loop and I´m do getting outpout 53145 for 100000, when the expected is 53144. I will have to figurate out how to do the exact math operation. Regards
26th Jan 2021, 10:43 AM
Enrique Guia
Enrique Guia - avatar
+ 1
Enrique Guia In case you haven't figured out a solution, making use of float variables outputs 10628.82 in my case and 53144.1 in your case .
26th Jan 2021, 11:09 AM
Abhay
Abhay - avatar
+ 1
@Simba: gracias
26th Jan 2021, 11:33 AM
Enrique Guia
Enrique Guia - avatar
+ 1
Use 90% for 6 months to find the balance. 4 lines of code added. https://code.sololearn.com/cp6GXE4Rc84v/?ref=app If you are paying 10% every month, one way is to find the 10% and then use your total minus the 10% paid to get the balance. The other way, is to find the 90% so you don't have to minus. If you have 100 and you paid 10%, Method 1: 100 - (100 * 10%) = 90 Method 2: 100 * 90% = 90 The "i=0" is used for looping. It will loop from 0 until 5 (< 6), which is 6 times. Finally prints out amount.
28th Jan 2021, 1:07 AM
Lam Wei Li
Lam Wei Li - avatar
+ 1
Enrique Guia 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:11 AM
Lam Wei Li
Lam Wei Li - avatar
0
Thanks for your advice, but at the end of the problem there is the following requirement: -- Use a loop to calculate the payment and remaining amounts for each month. Also, use integers for amounts. --
26th Jan 2021, 11:31 AM
Enrique Guia
Enrique Guia - avatar
0
@Lam Thanks for the explanation.
28th Jan 2021, 9:42 AM
Enrique Guia
Enrique Guia - avatar