Loan repayment calculator

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 3
Reputation: rudypooh786 is an unknown quantity at this point 
Solved Threads: 0
rudypooh786 rudypooh786 is offline Offline
Newbie Poster

Loan repayment calculator

 
0
  #1
May 11th, 2009
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.
  1. import java.util.Scanner;
  2. public class RepayCal{
  3. public static void main (String [] args){
  4. Scanner input= new Scanner(System.in);
  5.  
  6. System.out.println("Please enter the initial amount loaned> ");
  7. double InitialAmountLoan = input.nextDouble();
  8.  
  9. System.out.println("Please enter the annual interest rate> ");
  10. double AnnualInterestRate = input.nextDouble();
  11.  
  12. System.out.println("Please enter the monthly repayment amount> ");
  13. double MonthlyRepayment = input.nextDouble();
  14.  
  15. double InitialBalance = +InitialAmountLoan;
  16. System.out.println("Initial balance = " +InitialBalance );
  17. System.out.println();
  18.  
  19. double AmountOfInterest =0;
  20.  
  21. double MonthlyRate = (+AnnualInterestRate/100) /12;
  22.  
  23. double Balance =InitialAmountLoan + (+InitialAmountLoan * +MonthlyRate) - +MonthlyRepayment;
  24.  
  25. int count = 1;
  26.  
  27. System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment );
  28. System.out.println();
  29.  
  30. while (Balance > 0){
  31. if (Balance > 100)
  32. Balance = Balance + (+Balance * +MonthlyRate) - +MonthlyRepayment;
  33.  
  34. else if (Balance <100)
  35. Balance = 0;
  36.  
  37. count = count +1;
  38.  
  39. System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment );
  40. System.out.println();
  41.  
  42.  
  43. }
  44. System.out.println("Loan will take " +count+ " months to pay off a lone of " +InitialAmountLoan);
  45. }
  46. }


The outcome i achieve with the above code is this:

Loan repayment calculator

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
Initial balance = 1000.0
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Loan repayment calculator

 
0
  #2
May 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: rudypooh786 is an unknown quantity at this point 
Solved Threads: 0
rudypooh786 rudypooh786 is offline Offline
Newbie Poster

Re: Loan repayment calculator

 
0
  #3
May 12th, 2009
Thanks very much for your response but i have already been trying to work on a solution like that and had no luck if there was any chance someone could give me some example code it would be very helpful.

Many Thanks'
Ash
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: ramborambo is an unknown quantity at this point 
Solved Threads: 0
ramborambo ramborambo is offline Offline
Newbie Poster

Re: Loan repayment calculator

 
-1
  #4
May 12th, 2009
Do it yourself!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,718
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 230
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Loan repayment calculator

 
0
  #5
May 12th, 2009
Originally Posted by ramborambo View Post
Do it yourself!!
That was very rude.
You judge others when yourself have created 2 identical threads with the same question.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,658
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Loan repayment calculator

 
0
  #6
May 12th, 2009
  1. else if (Balance <100)
  2. MonthlyRepayment = Balance;
  3. Balance = 0;
  4. count = count +1;
  5. System.out.println("Balance at Month " +count+ " is " +Balance+ " after making a payment of " +MonthlyRepayment );
  6. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 371 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC