| | |
Loan repayment calculator
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2009
Posts: 3
Reputation:
Solved Threads: 0
Write a Java application that calculates the number of repayments for a loan based on the initial loan amount, annual interest rate and monthly payment. The user enters these values and application uses a loop to display the balance each month after payment has been made. Note that interest is charged each month on the remaining balance.
For this exercise, you need to use a while loop that repeats as long as the balance is greater than zero to calculate the new balance with interest after each payment. The body of the loop calculates the new balance plus monthly interest (monthly interest = annual interest rate / 12) and then subtracts the payment. Note that it may be possible for the last payment to be less than the usual monthly payment. In this case you need to take this into account by only subtracting the amount necessary to clear the outstanding balance and not the full monthly payment.
This is the code i have produced at the moment as my attempt at a soloution.
The outcome i achieve with the above code is this:
Balance at month 1 is £908.33 after making a payment of £100.00
Balance at month 2 is £815.90 after making a payment of £100.00
Balance at month 3 is £722.70 after making a payment of £100.00
Balance at month 4 is £628.72 after making a payment of £100.00
Balance at month 5 is £533.96 after making a payment of £100.00
Balance at month 6 is £438.41 after making a payment of £100.00
Balance at month 7 is £342.07 after making a payment of £100.00
Balance at month 8 is £244.92 after making a payment of £100.00
Balance at month 9 is £146.96 after making a payment of £100.00
Balance at month 10 is £48.18 after making a payment of £100.00
Balance at month 11 is £0.00 after making a payment of £100.00
Loan will take 11 months to payoff.
As you can see on the final line it should say payment of £48.18 for month 11 but instead it comes out with £100.00. It would be much appreciated if some one could assit me and amend my current coding to get the correct solution.
Many Thanks,
Ash
For this exercise, you need to use a while loop that repeats as long as the balance is greater than zero to calculate the new balance with interest after each payment. The body of the loop calculates the new balance plus monthly interest (monthly interest = annual interest rate / 12) and then subtracts the payment. Note that it may be possible for the last payment to be less than the usual monthly payment. In this case you need to take this into account by only subtracting the amount necessary to clear the outstanding balance and not the full monthly payment.
This is the code i have produced at the moment as my attempt at a soloution.
java Syntax (Toggle Plain Text)
import java.util.Scanner; public class RepayCal{ public static void main (String [] args){ Scanner input= new Scanner(System.in); System.out.println("Please enter the initial amount loaned> "); double InitialAmountLoan = input.nextDouble(); System.out.println("Please enter the annual interest rate> "); double AnnualInterestRate = input.nextDouble(); System.out.println("Please enter the monthly repayment amount> "); double MonthlyRepayment = input.nextDouble(); double InitialBalance = +InitialAmountLoan; System.out.println("Initial balance = " +InitialBalance ); System.out.println(); double AmountOfInterest =0; double MonthlyRate = (+AnnualInterestRate/100) /12; double Balance =InitialAmountLoan + (+InitialAmountLoan * +MonthlyRate) - +MonthlyRepayment; int count = 1; System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment ); System.out.println(); while (Balance > 0){ if (Balance > 100) Balance = Balance + (+Balance * +MonthlyRate) - +MonthlyRepayment; else if (Balance <100) Balance = 0; count = count +1; System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment ); System.out.println(); } System.out.println("Loan will take " +count+ " months to pay off a lone of " +InitialAmountLoan); } }
The outcome i achieve with the above code is this:
Loan repayment calculatorInitial balance = 1000.0
Please enter the initial amount to loan> £1000
Please enter the annual interest rate, e.g. 28.9> 10
Please enter the monthly payment amount> 100
Balance at month 1 is £908.33 after making a payment of £100.00
Balance at month 2 is £815.90 after making a payment of £100.00
Balance at month 3 is £722.70 after making a payment of £100.00
Balance at month 4 is £628.72 after making a payment of £100.00
Balance at month 5 is £533.96 after making a payment of £100.00
Balance at month 6 is £438.41 after making a payment of £100.00
Balance at month 7 is £342.07 after making a payment of £100.00
Balance at month 8 is £244.92 after making a payment of £100.00
Balance at month 9 is £146.96 after making a payment of £100.00
Balance at month 10 is £48.18 after making a payment of £100.00
Balance at month 11 is £0.00 after making a payment of £100.00
Loan will take 11 months to payoff.
As you can see on the final line it should say payment of £48.18 for month 11 but instead it comes out with £100.00. It would be much appreciated if some one could assit me and amend my current coding to get the correct solution.
Many Thanks,
Ash
Last edited by Narue; May 11th, 2009 at 4:35 pm. Reason: fixed code tags
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
else if (Balance <100)
Balance = 0;
count = count +1;
System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment );
That's because you set MonthlyRepayment to 100 initially and then you never set the variable to anything else. If you want to continuously pay off 100 except when there is less than that amount remaining, calculate the amount remaining and then set MonthlyRepayment to that amount for the last payment.
Balance = 0;
count = count +1;
System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment );
That's because you set MonthlyRepayment to 100 initially and then you never set the variable to anything else. If you want to continuously pay off 100 except when there is less than that amount remaining, calculate the amount remaining and then set MonthlyRepayment to that amount for the last payment.
•
•
Join Date: Sep 2008
Posts: 1,658
Reputation:
Solved Threads: 206
Java Syntax (Toggle Plain Text)
else if (Balance <100) MonthlyRepayment = Balance; Balance = 0; count = count +1; System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment ); System.out.println();
All I added was MonthlyRepayment = Balance; if you don't understand why that should work, just ask.
Last edited by BestJewSinceJC; May 12th, 2009 at 3:34 pm.
![]() |
Similar Threads
- There is an elephant on the loo! (Geeks' Lounge)
- What is E-commerce? (eCommerce)
- Mortgage Calculator (C++)
Other Threads in the Java Forum
- Previous Thread: recursion sierpinskys triangle - HELP!!
- Next Thread: JFreeChart
Views: 371 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application arguments array arrays automation bidirectional binary birt bluetooth calculator chat class classes client code columns component database designadrawingapplicationusingjavajslider detection draw eclipse editor error errors event exception expand file fractal game givemetehcodez graphics gui guidancer helpwithhomework html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jmf jni jpanel julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie number object oracle os plazmic print problem program programming project recursion scanner screen server set signing size smart sms smsspam socket sort sql string subclass support swing test threads time transfer tree windows






