- 2

Loan Calculator problem in java. What is the error in my code? It passes 3 out of 5 test cases.Please help me.

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 double amt = amount*0.1; double ramt = amount - amt ; double amt1 = ramt*0.1; double ramt1 = ramt-amt1; double amt2 = ramt1 *0.1; double ramt2 = ramt1 -amt2; double amt3 = ramt2 * 0.1; double ramt3 = ramt2 - amt3 ; double amt4 = ramt3 *0.1; double ramt4 = ramt3-amt4; double amt5 = ramt4*0.1; double ramt5 = ramt4- amt5; int x = (int)ramt5 ; System. out.println ( x ); } } https://www.sololearn.com/discuss/2602294/?ref=app https://www.sololearn.com/discuss/2602294/?ref=app

25th Nov 2020, 9:47 PM
shafiq
shafiq - avatar
5 Answers
+ 4
shafiq your question appears unclear or as if this is a code coach solutions question. If this is the second, code coach solutions are for you to discover your strengths and weaknesses and to work with like a rubik's cube. If the community does it for you then what have you learned other than to have others do your work for you. I encourage you to look at your code and to see if you missed something in the wording of the challenge itself. https://code.sololearn.com/c286V5MWP9N1/?ref=app
25th Nov 2020, 11:34 PM
BroFar
BroFar - avatar
+ 1
Hi,BroFar.Yes, finally I did it.Problem was as follows: 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 Here is the monthly payment schedule: 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 My code is as follows: double sum = (double) amount; for(int i=1;i<=6;i++) { sum =Math.floor( sum-(sum*0.1)); } int sum1 = (int)sum; System.out.println(sum1);
27th Nov 2020, 7:23 PM
shafiq
shafiq - avatar
+ 1
Thanks to Benjamin.
27th Nov 2020, 7:31 PM
shafiq
shafiq - avatar
0
You can just calculate amount * 0.9^6 Maybe the error is because you are rounding wrong. Int cast is like floor rounding
26th Nov 2020, 12:16 AM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
0
How is that post that you attached related to this?
26th Nov 2020, 12:18 AM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar