# Mortgage calculator

Page 1 of 1

## 1 Replies - 2518 Views - Last Post: 07 December 2009 - 09:02 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144100&amp;s=72d79b9d4ab0e9cd95a38f92484d20c7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sernitydragonfly

Reputation: 0
• Posts: 1
• Joined: 07-December 09

# Mortgage calculator

Posted 07 December 2009 - 06:11 PM

I am really stuck on the equation to calculate the different mortgage payments. any help would be greatly appreciated.

this section in the code below:
double Payment1= Math.round(loanAmount*(interestMonthly[1]/(1-Math.pow((1 + interestMonthly[1])-loanTerm[1])))*100);
Payment1=(Payment1/100);
double Payment2= Math.round(loanAmount*(interestMonthly[2]/(1-Math.pow((1 + interestMonthly[2])-loanTerm[2])))*100);
Payment2=(Payment2/100);
double Payment3= Math.round(loanAmount*(interestMonthly[3]/(1-Math.pow((1 + interestMonthly[3])-loanTerm[3])))*100);
Payment3=(Payment3/100);

//packages imported for class and method use
import java.io.*;
import java.util.Date;
import java.text.DecimalFormat;

public class MortgageCalculatorV3
{

public static void main(String[] args)
{

DecimalFormat decimalPlace=new DecimalFormat("\$000,000.00");

//Declares and builds the variables
double[] loanTerm= new double[3];
loanTerm[1]=7*12;
loanTerm[2]=15*12;
loanTerm[3]=30*12;

double[] interestMonthly=new double [3];
interestMonthly[1]=(.0535/12);
interestMonthly[2]=(.055/12);
interestMonthly[3]=(.0575/12);

double loanAmount = 200000;
double monthlyPayment = 0;

//Displays the mortgage calculator
System.out.println("Mortgage Calculator");
System.out.println("The loan amount is \$200,000");
System.out.println();

//Declares and builds three new variables
double loanBalance = 0;
double interestPaid = 0;
int lineCount = 20;
loanBalance = loanAmount - monthlyPayment;

//Starts loop statement,and declares formula for loan balance and interest paid
while (loanBalance > 0) {

//Declares formula
double Payment1= Math.round(loanAmount*(interestMonthly[1]/(1-Math.pow((1 + interestMonthly[1])-loanTerm[1])))*100);
Payment1=(Payment1/100);
double Payment2= Math.round(loanAmount*(interestMonthly[2]/(1-Math.pow((1 + interestMonthly[2])-loanTerm[2])))*100);
Payment2=(Payment2/100);
double Payment3= Math.round(loanAmount*(interestMonthly[3]/(1-Math.pow((1 + interestMonthly[3])-loanTerm[3])))*100);
Payment3=(Payment3/100);

//Displays the monthly payment for each option.
System.out.println("Mortgage payment for 7 years is " + decimalPlace.format(Payment1));
System.out.println("Mortgage payment for 15 years is " + decimalPlace.format(Payment2));
System.out.println("Mortgage payment for 30 years is " + decimalPlace.format(Payment3));

loanBalance = loanBalance - monthlyPayment;
interestPaid = (monthlyPayment * interestMonthly);

//Pauses screen
if (lineCount > 0) {
lineCount--;
try {
catch (InterruptedException e) {
}
}
}
//Stops loop statement
if (loanBalance <= 0) {
System.out.println("The loan balance is: \$0.00");
}
}

Is This A Good Question/Topic? 0

## Replies To: Mortgage calculator

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Mortgage calculator

Posted 07 December 2009 - 09:02 PM

You are really looking for problems if you use such statements

double Payment1= Math.round(loanAmount*(interestMonthly[1]/(1-Math.pow((1 + interestMonthly[1])-loanTerm[1])))*100);

and they are impossible to debug..... and think at the poor programmer that will have to maintain your code
split your different steps... something like that: (I am not sure your formula is impossible to read)

double intCalc = 1 + interestMonthly[1]-loanTerm[1];
double pow = 1 - Math.pow(intCalc);
double tmpLoan= loanAmount * pow;
.... you can alternatively print intCalc and pow and tmpLoan
double Payment1 = Math.round(tmpLoan * 100);