What is wrong in this code? | Sololearn: Learn to code for FREE!
+ 1

What is wrong in this code?

Q..... You take a loan from a friend and need to calculate how much you will owe him after 3 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 3 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 My code..... 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 amount = 20000; int Payment = (20000 * 10)/100; do{ System.out.println("pay:"Payment); amount = amount - payment; } while(amount >= 0); } }

1st Jan 2022, 6:17 AM
Bidhan Kumar Paul
Bidhan Kumar Paul - avatar
1 Answer
+ 6
First, u need to include the payment in the do-while loop because u need to calculate the payment every month. Next, output your result after calculation. Lastly, The condition should be cater for the months instead of the amount. Solution: import java.util.Scanner; public class Program { public static void main(String[] args) { int month = 3; Scanner scanner = new Scanner(System.in); int amount = scanner.nextInt(); //your code goes here int payment; do{ payment = (amount * 10)/100; amount = amount - payment; System.out.println("pay:" + amount); month--; } while(month > 0); } } Of course, the remaining part I'll leave it for u to work out
1st Jan 2022, 6:59 AM
Chan Jia Min
Chan Jia Min - avatar