# Monthly mortgage payment calculator

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• March 24th, 2013, 08:22 AM
iamgonge
Monthly mortgage payment calculator
IM writing a monthly mortgage payment calculator I have the formula but dont know how to enter it:

the formula i have is

pmt = ((p-d)*r/12) / (1-(1+(r/12)^-m)

my code is:
import java.util.Scanner;

public class Project1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter the price of the house: ");
double housePrice = input.nextDouble();
System.out.println("Enter the down payment: ");
double downPayment = input.nextDouble();
System.out.println("Enter the annual interest rate: ");
double annualInterestRate = input.nextDouble();
System.out.println("Enter the number of payments: ");
double numberofPayments = input.nextDouble();

double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(annualInterestRate/12)^-numberofPayments))));

}
}
I dont know how to enter this formula maybe there is a simpler way?

--- Update ---

In which PMT is the monthly payment, P is the listed price of the house, D is the
down payment, r is the annual interest rate and m is the number of payments.
• March 24th, 2013, 08:25 AM
curmudgeon
Re: Monthly mortgage payment calculator

[code]

[/code]

Also, regarding your formula, have you had a chance to look at some of the methods available in the Math class? Some will come in handy for you such as Math.pow(....). Please have a look as you won't regret it!
• March 24th, 2013, 08:28 AM
iamgonge
Re: Monthly mortgage payment calculator
Code :

```      import java.util.Scanner;   public class Project1 { public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.println("Enter the price of the house: "); double housePrice = input.nextDouble(); System.out.println("Enter the down payment: "); double downPayment = input.nextDouble(); System.out.println("Enter the annual interest rate: "); double annualInterestRate = input.nextDouble(); System.out.println("Enter the number of payments: "); double numberofPayments = input.nextDouble();   double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(annualInterestRate/12)^-numberofPayments))));       System.out.println( (monthlyPayment+ " is the larger of the two numbers.");       } }```

also Im somewhat aware of the math pow function but dont know how to fit it in this formula(not sure where it would go)
• March 24th, 2013, 08:31 AM
curmudgeon
Re: Monthly mortgage payment calculator
Quote:

Originally Posted by iamgonge
also Im somewhat aware of the math pow function but dont know how to fit it in this formula(not sure where it would go)

Anywhere you see the little ^ in your math formula this can mean x to the power of y, while in Java it means something completely different (and not very useful for your purposes). I'd start there. Good luck!
• March 24th, 2013, 09:00 AM
iamgonge
Re: Monthly mortgage payment calculator
ok tried it didnt work now what( I dont know how to write this code!!!!!!)
heres what i tried

double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(annualInterestRate/12)math.pow-numberofPayments)))); <----dont know how to enter this last part its a negative exponent

--- Update ---

ok i think i got it does this look right?
double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, - numberofPayments)))));
• March 24th, 2013, 09:01 AM
curmudgeon
Re: Monthly mortgage payment calculator
The Math API will tell you how to use the method, but as in calling *any* method, you will need to use parentheses. You will need to separate your parameters by commas if you have multiple parameters.

As an aside: please do not multi-post the same question in the forum.

Quote:

Originally Posted by iamgonge
ok i think i got it does this look right?
double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, - numberofPayments)))));

Test it out and see if the code works as expected.
• March 24th, 2013, 09:24 AM
iamgonge
Re: Monthly mortgage payment calculator
so it doesnt seem to be computing correctly i keep getting negative number and they seem quite off, Is my formula wrtitten wrong?

--- Update ---

ok so this my program as of this moment, it runs but the math is wrong can someone please check my formula
[code]

import java.util.Scanner;

public class Project1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("Enter the price of the house: ");
double housePrice = input.nextDouble();
System.out.println("Enter the down payment: ");
double downPayment = input.nextDouble();
System.out.println("Enter the annual interest rate: ");
double annualInterestRate = input.nextDouble();
System.out.println("Enter the number of payments: ");
double numberofPayments = input.nextDouble();

double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, -numberofPayments)))));

System.out.println( "The price of the house is: " + housePrice);
System.out.println( "Your down payment is :" + downPayment);
System.out.println( "Your annual interest rate is :" + annualInterestRate);
System.out.println( "Your number of payments are :" + numberofPayments);
System.out.println( "Your monthly payment is :" + monthlyPayment);

}
}
/code
• March 24th, 2013, 09:24 AM
Norm
Re: Monthly mortgage payment calculator
Quote:

it doesnt seem to be computing correctly
...
check my formula
Try debugging the code by breaking the compound expression up into separate single expressions and printing the results of each. The print out should show you where the code is going wrong.
• March 24th, 2013, 09:38 AM
iamgonge
Re: Monthly mortgage payment calculator
double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, -numberofPayments)))));

• March 24th, 2013, 09:44 AM
Norm
Re: Monthly mortgage payment calculator
Try debugging the code by breaking the compound expression up into separate single expressions and printing the results of each. The print out should show you where the code is going wrong.
• March 24th, 2013, 09:56 AM
iamgonge
Re: Monthly mortgage payment calculator
ok changed it still doesnt work please check

Code :

```    import java.util.Scanner;   public class Project1 { public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.println("Enter the price of the house: "); double housePrice = input.nextDouble(); System.out.println("Enter the down payment: "); double downPayment = input.nextDouble(); System.out.println("Enter the annual interest rate: "); double annualInterestRate = input.nextDouble(); System.out.println("Enter the number of payments: "); double numberofPayments = input.nextDouble();   double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+(Math.pow(annualInterestRate/12, -numberofPayments))))));     System.out.println( "The price of the house is: " + housePrice); System.out.println( "Your down payment is :" + downPayment); System.out.println( "Your annual interest rate is :" + annualInterestRate); System.out.println( "Your number of payments are :" + numberofPayments); System.out.println( "Your monthly payment is :" + monthlyPayment);               } }```
• March 24th, 2013, 10:03 AM
curmudgeon
Re: Monthly mortgage payment calculator
Please edit your post above and fix your code tags. Your code is difficult to read. If it doesn't work, then please show what you've done to try to debug it (as per Norm's recommendations above), and the results of these efforts.
• March 24th, 2013, 10:04 AM
Norm
Re: Monthly mortgage payment calculator
Quote:

ok changed it still doesnt work please check
Did you do what I suggested to find the problem? I don't see it in the posted code.

Try debugging the code by breaking the compound expression up into separate single expressions and printing the results of each. The print out should show you where the code is going wrong.
• March 24th, 2013, 10:11 AM
iamgonge
Re: Monthly mortgage payment calculator
im not faqmiliar with this formula i dont know how to break it down in java code so i cant do what your saying are you saying assign this a variable (housePrice - downPayment) then assign this a variable (annualInterestRate/12)) and so on, ? I dont understand
• March 24th, 2013, 10:14 AM
Norm
Re: Monthly mortgage payment calculator
Quote:

saying assign this a variable (housePrice - downPayment) then assign this a variable (annualInterestRate/12)) and so on
Yes, that is exactly what I'm saying.
• March 24th, 2013, 10:22 AM
iamgonge
Re: Monthly mortgage payment calculator
ok this what i got i broke the formula into a top and a bottom
then printed out the results: heres the code
Code :

```    import java.util.Scanner;   public class Project1 { public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.println("Enter the price of the house: "); double housePrice = input.nextDouble(); System.out.println("Enter the down payment: "); double downPayment = input.nextDouble(); System.out.println("Enter the annual interest rate: "); double annualInterestRate = input.nextDouble(); System.out.println("Enter the number of payments: "); double numberofPayments = input.nextDouble();     double tempTop = ((housePrice - downPayment) * (annualInterestRate/12)); double tempBottom = ((1-(1+(Math.pow(annualInterestRate/12, -numberofPayments))))); double monthlyPayment = (tempTop / tempBottom); System.out.println( "The price of the house is: " + housePrice); System.out.println( "Your down payment is :" + downPayment); System.out.println( "Your annual interest rate is :" + annualInterestRate); System.out.println( "Your number of payments are :" + numberofPayments); System.out.println( "Your monthly payment is :" + monthlyPayment); System.out.println( ""); System.out.println( "Top of equation equals :" + tempTop);   System.out.println( "Bottom of equation equals :" + tempBottom);     } }```
• March 24th, 2013, 10:24 AM
Norm
Re: Monthly mortgage payment calculator
Quote:

then printed out the results:
When you look at the values that were printed out, were they correct?
• March 24th, 2013, 10:26 AM
iamgonge
Re: Monthly mortgage payment calculator
no
• March 24th, 2013, 10:31 AM
Norm
Re: Monthly mortgage payment calculator
Which ones were wrong?
What are the the correct values?

Which simple, single expression computed the wrong value?
• March 24th, 2013, 10:33 AM
StephenCoyle
Re: Monthly mortgage payment calculator
Quote:

Originally Posted by iamgonge
double monthlyPayment= ( ((housePrice - downPayment) * (annualInterestRate/12)) /((1-(1+Math.pow(annualInterestRate/12, -numberofPayments)))));

Even try solving the problem, using a pen and paper. Make sure that its correct. For example :
housePrice = 30000.00
downPayment = 5000.00

then write it out like ( ((30000.00 - 50000.00) * ...

make sure you get the result you expect. Then compare this result with Java programs result.
• March 24th, 2013, 10:37 AM
iamgonge
Re: Monthly mortgage payment calculator
ok i did the calculations on a calculator and they do equal each other, I bottom number keeps turning back a negative number which i dont think is right.

the formula im supposed to use is written on my paper like this PMT = (P - D)*r/12
__________
(1-(1+r/12)^-m) where P is the listed price of the house, D is the
down payment, r is the annual interest rate and m is the number of payments.
• March 24th, 2013, 10:40 AM
Norm
Re: Monthly mortgage payment calculator
Which expression returns the wrong value? and what value does it return?
• March 24th, 2013, 10:42 AM
iamgonge
Re: Monthly mortgage payment calculator
the bottom returns a negative which then turns the final number to a negative monthly payments cant be negative so something is wrong
• March 24th, 2013, 10:45 AM
Norm
Re: Monthly mortgage payment calculator
Can you post the code for "the bottom" expression and the value it returned?
also print out the values of the variables used in "the bottom" expression.
• March 24th, 2013, 10:51 AM
StephenCoyle
Re: Monthly mortgage payment calculator
PMT = (P - D)*r/12
______________
(1-(1+r/12)^-m)

Ok this is the formula. Do me a favour and post up what values you are using and the expected result.
Also annualInterestRate, is that a percentage % or a monetary value (£, \$).
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last