+ 4

Java Loan Calculator

Hi all, I was wondering if someone would be able to help me with my loan calculator. Test case 1 and 2 are fine but 3,4,5 are failing. These cases are hidden, so I can't work out what's going on. It's mostly probably really obvious but I'm new to this and just trying get my head around it. Thank you. https://code.sololearn.com/csHwmRhggPvu/?ref=app

15th Feb 2022, 7:11 AM
M H
M H - avatar
5 odpowiedzi
+ 4
I don't understand the complex calculation you're trying to do here. The question was to deduct 10% from the amount 3 times. Yo do that you can use a for loop as below: for(int i=0;i<3;++i){ amount-=0.1*amount; } 10% of x can be calculated by (10/100)*x, thus 0.1*x
15th Feb 2022, 7:29 AM
Rishi
Rishi - avatar
+ 2
amount/100*10 differences with amount*10/100 in result. You need there in problem is amount*10/100. Your if condition is always true, unless is 0 or negetive. There dont exist such input. And you are making same calculating in do while.. So you can just do loop 3 times with remaining=amount; without if block, or directly using 'amount' instead of 'remaining' of extra variable. That's make code simple readable.. Hope it helps..
15th Feb 2022, 9:44 AM
Jayakrishna 🇮🇳
+ 2
Thanks, my original code worked with changing /100*10 to *10/100 and confirms I was on the right track but over complicated things. I've managed cut out some unnecessary variables too. Both answers have been very helpful and gave me a lot better understanding. I know the 1st answer is the shortest way of writing this code but I like how the code reads in my answer. https://code.sololearn.com/cfTyNcEodCWc/?ref=app
15th Feb 2022, 12:25 PM
M H
M H - avatar
+ 1
Thank you for your help. I think I've over complicated the calculation and confused myself.
15th Feb 2022, 7:40 AM
M H
M H - avatar
+ 1
System.out.println(((amount-amount*10/100)-(amount-amount*10/100)*10/100)-((amount-amount*10/100)-(amount-amount*10/100)*10/100)*10/100)  my try on code golfing this
15th Feb 2022, 2:47 PM
Jalaj Madaan
Jalaj Madaan - avatar