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.

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 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

Recommended Answers

All 7 Replies

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.

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

Do it yourself!!

commented: Rude non-productive comment -1

Do it yourself!!

That was very rude.
You judge others when yourself have created 2 identical threads with the same question.

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.

This independent calculator shows what your monthly payments would be for a given loan, where interest is compounded monthly.

It doesn't work at all!!!
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.