Loan Calculator- When I execute the code, it passess the first two tests only. What am I missing here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Loan Calculator- When I execute the code, it passess the first two tests only. What am I missing here?

I know you can solve this in many ways and maybe in more efficient ways, but this is the first way I think of. 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 firstmonth=0; int secondmonth=0; int thirdmonth=0; int remainingTotal=0; for(int months=1; months<=3; months++) { if(months==1) { int paymentOne = amount/100 *10; int remainingAmount = amount - paymentOne; firstmonth = remainingAmount; } if(months==2) { int paymenTwo=firstmonth/100 *10; secondmonth = firstmonth-paymenTwo; } if(months==3){ int paymenThree = secondmonth/100 *10; thirdmonth = secondmonth-paymenThree; } } remainingTotal = thirdmonth; System.out.println(remainingTotal); } }

21st Jan 2022, 7:05 PM
Ans
4 Answers
+ 3
Try your code with input 90. The result becomes 90. And that's wrong. You mistake is in lines like: paymentOne = amount/100*10 Because of integer calculation becomes amount/100 => zero for small amounts. And multiplied by 10 it's also zero. Change this lines to: paymentOne = amount*10/100
21st Jan 2022, 7:19 PM
Coding Cat
Coding Cat - avatar
+ 2
Try in all Amount*10/100 instead of Amount/100*10
21st Jan 2022, 7:16 PM
Jayakrishna 🇮🇳
+ 2
Its working, thank you all! 😊
21st Jan 2022, 7:46 PM
Ans
+ 1
you are making it very long and complicated, I have it done in 1 for + 1 line <del> besides, if I remember correctly, it is better to use "*0.1" than "/100*10" There's something there with the decimals, for some reason you lose precision. Again, I don't remember if it was the case for java.😅😅 </del> Edit: it wasn't the case for java, see the other answers.
21st Jan 2022, 7:17 PM
CGM
CGM - avatar