public static void calculateMonthlyPayment()
{
//First you must define some variables to make it easier to set up:
//P = principal, the initial amount of the loan
double dblP = 200000.00;
//I = the annual interest rate (from 1 to 100 percent)
double dblI = 5.75;
//L = length, the length (in years) of the loan, or at least the length over which the loan is amortized.
int intL = 30;
//The following assumes a typical conventional loan where the interest is compounded monthly. First I will define two more variables to make the calculations easier:
//J = monthly interest in decimal form = I / (12 x 100)
double dblJ = dblI / (12 * 100);
//N = number of months over which loan is amortized = L x 12
int intN = intL * 12;
//Okay now for the big monthly payment (M) formula, it is:
// M = P * (J/ (1 - (1 + J)^ -N)
// J
// M = P x ------------------------
//
// 1 - ( 1 + J ) ^ -N
//
//where 1 is the number one (it does not appear too clearly on some browsers)
double dblM = dblP * (dblJ / (1 - Math.pow((1 + dblJ), - intN)));
System.out.println("Monthly Payment = $" +dblM+ "\n");
// So to calculate it, you would first calculate 1 + J then take that to the -N (minus N) power, subtract that from the number 1. Now take the inverse of that (if you have a 1/X button on your calculator push that). Then multiply the result times J and then times P. Sorry, for the long way of explaining it, but I just wanted to be clear for everybody.
// The one-liner for a program would be (adjust for your favorite language):
// M = P * ( J / (1 - (1 + J) ** -N))