Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Monthly mortgage payment calculator

1. ## 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.  Reply With Quote

3. ## 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!  Reply With Quote

4. ## Re: Monthly mortgage payment calculator

```

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)  Reply With Quote

5. ## Re: Monthly mortgage payment calculator 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!  Reply With Quote

6. ## 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)))));  Reply With Quote

7. ## 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. 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.  Reply With Quote

8. ## 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  Reply With Quote

9. ## Re: Monthly mortgage payment calculator

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.  Reply With Quote

10. ## Re: Monthly mortgage payment calculator

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

11. ## 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.  Reply With Quote

12. ## Re: Monthly mortgage payment calculator

ok changed it still doesnt work please check

```

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

}
}```  Reply With Quote

13. ## 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.  Reply With Quote

14. ## Re: Monthly mortgage payment calculator

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.  Reply With Quote

15. ## 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  Reply With Quote

16. ## Re: Monthly mortgage payment calculator

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.  Reply With Quote

17. ## 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
```

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

}
}```  Reply With Quote

18. ## Re: Monthly mortgage payment calculator

then printed out the results:
When you look at the values that were printed out, were they correct?  Reply With Quote

19. ## Re: Monthly mortgage payment calculator

no  Reply With Quote

20. ## Re: Monthly mortgage payment calculator

Which ones were wrong?
What are the the correct values?

Which simple, single expression computed the wrong value?  Reply With Quote

21. ## Re: Monthly mortgage payment calculator 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.  Reply With Quote

22. ## 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.  Reply With Quote

23. ## Re: Monthly mortgage payment calculator

Which expression returns the wrong value? and what value does it return?  Reply With Quote

24. ## 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  Reply With Quote

25. ## 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.  Reply With Quote

26. ## 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 (£, \$).  Reply With Quote