Click here to Skip to main content
15,891,993 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a program that lets the user input a loan amount and starting annual interest rate
(not monthly interest rate), then display the monthly and total payments. Print a table that
displays the monthly and total payments for 4 interest rates, with an increment of ½
starting from the given starting interest rate for the years from 1 to 10 (if pay off the
mortgage in that number of years.)
The formula to compute the monthly payment is as follows:
monthlyPayment = (loanAmount * monthlyInterestRateAdd)/(1-Math.pow(1+monthlyInterestRateAdd,-12));

the output is to look like this:
Enter the loan amount: 10000
Enter the starting interest rate: 5.0
Years Interest Rate Monthly Payment Total Payment
1        5%           856.07           10272.89
         5.5%           858.36           10300.41
         6.0%         860.66           10327.97
         6.5%         862.96             10355.57
2        5%            438.71           10529.13
         5.5%          440.95           10582.95
         6.0%         443.2             10636.94
         6.5%         445.46            10691.1
… …
10          5%        106.06            12727.86
          5.5%        108.52            13023.15
          6.0%        111.02            13322.46
          6.5%        113.54            13625.75
My code looks like this:
Java
import java.util.Scanner;
import java.text.NumberFormat;

public class MortgageCalculator {

    public static void main(String[] args) {
        // declare variables for main method
        double loanAmount;//double value loan amount 
        double annualInterestRate;//double value interest rate
        int loanDuration = 120;//int value for number of months 10 years
        double monthlyPayment, monthlyInterestRate, monthlyInterestRateAdd;
        double annualInterestRateAdd;
        //  double monthlyPaymentTotal = 0;
        double principle;
        
        int year = 1;
        Scanner keyboard = new Scanner(System.in);
        NumberFormat percent = NumberFormat.getPercentInstance();
        percent.setMaximumFractionDigits(3);
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        currency.setMaximumFractionDigits(2);
        System.out.println("Please enter the amount of your loan.");
        loanAmount = keyboard.nextDouble();
        
        System.out.println("Please enter the annual interest rate as a decimal. Ex. 7.5% = .075");
        annualInterestRate = keyboard.nextDouble();
        monthlyInterestRate = (annualInterestRate/12);
        System.out.println("Year    Interest Rate   Monthly Payment         Total Payment" );
        // double monthlyPaymentTotal;
        while (year < 11)
        {
            annualInterestRateAdd = annualInterestRate;
            monthlyInterestRateAdd = monthlyInterestRate;
            System.out.print(year);
            double monthlyPaymentTotal = 0; 
            for (int i = 1; i < 5; i += 1)
            {  
                System.out.print("\t"+ percent.format(annualInterestRateAdd));
                monthlyPayment = (loanAmount * monthlyInterestRateAdd)/(1-Math.pow(1+monthlyInterestRateAdd,-12));              
                System.out.print("\t\t"+ currency.format(monthlyPayment));
                monthlyPaymentTotal = monthlyPayment*12;
                System.out.println("\t\t\t" + currency.format(monthlyPaymentTotal));
                annualInterestRateAdd += .005;
                monthlyInterestRateAdd += monthlyInterestRate/12;
            }
            
            loanAmount = (loanAmount + (1+ monthlyInterestRate))- monthlyPaymentTotal ;   
            
            year += 1;
        }
    }
}

I canot get the subsequent years to cary the Totals and reduce the monthly payments and increase the total amount paid on the far right column any help is appreciated.
Posted
Updated 8-Jun-15 21:54pm
v2
Comments
Richard MacCutchan 9-Jun-15 4:00am    
This line monthlyInterestRateAdd += monthlyInterestRate/12; at the end of the for loop, looks wrong. Should it not be
monthlyInterestRateAdd = annualInterestRateAdd/12;?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900