# Yet Another Mortgage Calculator Problem

Page 1 of 1

## 1 Replies - 3111 Views - Last Post: 03 October 2010 - 02:59 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=193320&amp;s=c209104747c13449e50c9ba6ae1ec324&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hdavison4139

Reputation: 0
• Posts: 2
• Joined: 03-October 10

# Yet Another Mortgage Calculator Problem

Posted 03 October 2010 - 02:50 PM

OK this is not looking how it's supposed to look when i run it. Below is the code and the results from the run. Can someone please tell what i have missing or what's wrong with my code.

```/*Week 2 Assignment
Individual Assignment
Service Request SR-mf-003
Heather Davison
POS420
*/
package mortgagecalc;
/* A program written in Java that will calculate and display the monthly payment amount to a \$200,000.00 loan over a 30 year term at a 5.75‰ interest rate. */
import java.lang.Math.*;        /* Program uses the Math class to perform mathematical functions. */
import java.text.NumberFormat;  // formats the number as currency, with a decimal place and \$

public class Main //Beginning
{
public static void main(String[] args) //tells the program what to do
{
/* P = principal, the initial amount of the loan
I = the annual interest rate (from 1 to 100 percent)
L = length, the length (in years) of the loan, or at least the length over which the loan is amortized.
J = monthly interest in decimal form = I / (12 x 100)
N = number of months over which loan is amortized = L x 12
M = the monthly payment
M = P * ( J / (1 - (1 + J) ** -N))
Above are the definitions and terms to create an equation to calculate the mortgage payment.
http://www.hughchou.org/calc/formula.html
*/
double dblmonthlypayment; // payment
double dblprincipal=200000; // Amount of principal
int intyears=30; // Number of years in loan
int intnumberOfMonthsAmortize; // Number of Months
double dblinterest=5.75; // Interest Rate
double dblmonthlyInterest; // Monthly Interest Rate

NumberFormat nf = NumberFormat.getCurrencyInstance(); // formats the number as currency, with a decimal place and \$

dblmonthlyInterest = dblinterest/(12*100); //J monthly interest
intnumberOfMonthsAmortize = intyears*12;   //N number of months
//Declares formula
dblmonthlypayment = dblprincipal*(dblmonthlyInterest / (1 - Math.pow((1 + dblmonthlyInterest), -intnumberOfMonthsAmortize)));
System.out.println();                                    //Spacing
System.out.println("Mortgage Calculator");               //Display as is
System.out.println("Principal of the loan = \$200,000");  //Display as is
System.out.println("Loan length = 30 years");            //Display as is
System.out.println("Loan Interest = 5.75%");             //Display as is
+ nf.format(dblmonthlypayment));                    //Display as is
System.out.println();                                    //Spacing

//Declares and builds three new variables
double loanBalance = 0;
double interestPaid = 0;
int lineCount = 20;
int x=0;
loanBalance = dblprincipal - dblmonthlypayment;
interestPaid = loanBalance * dblmonthlyInterest;

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

//Displays the loan balance and interest paid
System.out.println("The loan balance is: " + nf.format(loanBalance));
System.out.println("The interest paid on the loan is: " + nf.format(interestPaid));

loanBalance = loanBalance - dblmonthlypayment;
interestPaid = loanBalance * dblmonthlyInterest;

/* This is the original calculation that I normally use to calculate payment
*  It generates a different payment than the one used in class but calculating the
*  correct payment in not the general idea here.
*  pay = cost * ((tax/(12*100))/(1-Math.pow(((tax/(12*100))+ 1),(-1*mins))));
*/
dblmonthlyInterest = dblinterest/(12*100);
dblmonthlypayment = dblprincipal * (dblmonthlyInterest/(1-Math.pow((dblmonthlyInterest + 1),(-intyears))));
for (x=1;x<=intyears;x++)
{

if (x % 6 == 0)
{
System.out.println("Press any key to continue");
try
{
} catch (Exception e)
{;}
}
System.out.println("Schedule\t" + x + "\t" + intnumberOfMonthsAmortize + "\t" + nf.format (dblprincipal) + "\t" +  nf.format(loanBalance));

}
}
}
}

```

*Results from run* is this how it's supposed to look????

Mortgage Calculator
Principal of the loan = \$200,000
Loan length = 30 years
Loan Interest = 5.75%

The loan balance is: \$198,832.85
The interest paid on the loan is: \$952.74
Schedule 1 360 \$200,000.00 \$197,665.71
Schedule 2 360 \$200,000.00 \$197,665.71
Schedule 3 360 \$200,000.00 \$197,665.71
Schedule 4 360 \$200,000.00 \$197,665.71
Schedule 5 360 \$200,000.00 \$197,665.71
Press any key to continue

Schedule 6 360 \$200,000.00 \$197,665.71
Schedule 7 360 \$200,000.00 \$197,665.71
Schedule 8 360 \$200,000.00 \$197,665.71
Schedule 9 360 \$200,000.00 \$197,665.71
Schedule 10 360 \$200,000.00 \$197,665.71
Schedule 11 360 \$200,000.00 \$197,665.71
Press any key to continue

Schedule 12 360 \$200,000.00 \$197,665.71
Schedule 13 360 \$200,000.00 \$197,665.71
Schedule 14 360 \$200,000.00 \$197,665.71
Schedule 15 360 \$200,000.00 \$197,665.71
Schedule 16 360 \$200,000.00 \$197,665.71
Schedule 17 360 \$200,000.00 \$197,665.71
Press any key to continue

Schedule 18 360 \$200,000.00 \$197,665.71
Schedule 19 360 \$200,000.00 \$197,665.71
Schedule 20 360 \$200,000.00 \$197,665.71
Schedule 21 360 \$200,000.00 \$197,665.71
Schedule 22 360 \$200,000.00 \$197,665.71
Schedule 23 360 \$200,000.00 \$197,665.71
Press any key to continue

Schedule 24 360 \$200,000.00 \$197,665.71
Schedule 25 360 \$200,000.00 \$197,665.71
Schedule 26 360 \$200,000.00 \$197,665.71
Schedule 27 360 \$200,000.00 \$197,665.71
Schedule 28 360 \$200,000.00 \$197,665.71
Schedule 29 360 \$200,000.00 \$197,665.71
Press any key to continue

Schedule 30 360 \$200,000.00 \$197,665.71
The loan balance is: \$197,665.71
The interest paid on the loan is: \$947.15
Schedule 1 360 \$200,000.00 \$190,492.47
Schedule 2 360 \$200,000.00 \$190,492.47
Schedule 3 360 \$200,000.00 \$190,492.47
Schedule 4 360 \$200,000.00 \$190,492.47
Schedule 5 360 \$200,000.00 \$190,492.47
Press any key to continue

Schedule 6 360 \$200,000.00 \$190,492.47
Schedule 7 360 \$200,000.00 \$190,492.47
Schedule 8 360 \$200,000.00 \$190,492.47
Schedule 9 360 \$200,000.00 \$190,492.47
Schedule 10 360 \$200,000.00 \$190,492.47
Schedule 11 360 \$200,000.00 \$190,492.47
Press any key to continue

MOD EDIT: When posting code...USE CODE TAGS!!!

Also, please use DESCRIPTIVE titles when posting topics. You posted this topic in the Java Forum, therefore we KNOW you need JAVA Help!

This post has been edited by JackOfAllTrades: 03 October 2010 - 02:56 PM

Is This A Good Question/Topic? 0

## Replies To: Yet Another Mortgage Calculator Problem

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Yet Another Mortgage Calculator Problem

Posted 03 October 2010 - 02:59 PM

Well I would have to say no. I mean, if you have a mortgage, you'd expect the principal to decrease somewhat with every payment? In your case, it looks like only the first payment is being deducted from the principal. Certainly you can figure out why, right?