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

Loan Calculator

Loan Calculator 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 The condition is that we must use integer data type to store the variables and their values and not double or float data types. The problem here is that integer data type won't give you the correct answer, according to my code. I did try declaring the variables with double data type and got the correct answer but the system isn't accepting it due the aforementioned condition. Any help would be much appreciated!

21st Apr 2021, 5:34 AM
Qudrat Jan
Qudrat Jan - avatar
5 Answers
+ 3
import java.util.*; public class Program{ public static void main(String []args){ int TOTAL_MONTHS = 3; Scanner input = new Scanner(System.in); System.out.print("Enter amount borrowed: "); double amountBorrowed = input.nextDouble(); double remainingAmount = 0; double returnedPerMonth = 0; while (TOTAL_MONTHS != 0) { returnedPerMonth = amountBorrowed * 0.1; remainingAmount = amountBorrowed - returnedPerMonth; amountBorrowed -= returnedPerMonth; TOTAL_MONTHS--; System.out.println("The remaining amount is: " + remainingAmount); } } }
21st Apr 2021, 5:40 AM
Qudrat Jan
Qudrat Jan - avatar
+ 2
Qudrat Jan The expected output is correct at 14580 unless your question is wrong. It cannot be 10628.
6th Jun 2021, 3:31 AM
Lam Wei Li
Lam Wei Li - avatar
0
Qudrat Jan Without attempts don't think to get help. So where is your attempts?
21st Apr 2021, 5:38 AM
A͢J
A͢J - avatar
0
Qudrat Jan Don't print anything except only value and also don't print inside loop. You have to print outside the loop.
21st Apr 2021, 5:43 AM
A͢J
A͢J - avatar
0
That should not be an issue at all since it's only a value. Additionally, I wanted to check the value of the remainingAmount variable to analyze the program.
21st Apr 2021, 5:46 AM
Qudrat Jan
Qudrat Jan - avatar