A program to calculate the amount of a periodic loan payment. The program prompts for the principal (the amount borrowed), the loan interest rate as an annual percentage rate (APR), and the loan duration in years. It assumes each period is one month, so it converts the APR to a periodic rate, the duration from years to periods, and calculates a monthly payment.
There are no simple or well-known test cases, so we must choose realistic values and calculate the payment with a calculator. We usually express the interest rate and loan duration in annual or yearly values. However, the formula uses the more general concept of "periods" or the number of payments per year, which could be annually (once per year), quarterly (four times per year), or monthly. For this example, we'll use monthly or twelve times per year. That means that we must convert the APR to a periodic interest rate of APR/12, and years to the number of periods: years*12.
P | R | N | payment | |
---|---|---|---|---|
Case 1 | 100000 | 8% = 0.08 | 30 | 733.76 | Case 2 | 100000 | 4% = 0.04 | 15 | 739.69 |
payment.cpp introduces two new formatting operations: setf and precision. Both operations are member functions of the cout (console output) object.
cout.setf(ios::fixed);
cout.precision(2);
Note that these two statements only need to run once; cout remains as configured until the program ends or other statements reconfigure it.
/* * mortgage -- calculates the amount of a monthly mortgage payment based on the * principal (i.e., the amount borrowed), the annual percentage rate (APR), and * the number of years to pay off the loan. * * The program assumes one monthly payment (i.e., one period = one month). * Therefore, the number of periods = the number of years * 12 and * the periodic interest rate = APR / 12. */ #include <iostream> #include <cmath> using namespace std; int main() { double p; // principal-- loan amount int years; // number of years of loan double apr; // annual percentage rate cout << "Please enter the principal: "; cin >> p; cout << "Please enter the APR: "; cin >> apr; cout << "Please enter the number of years: "; cin >> years; cout.setf(ios::fixed); // fixed point output cout.precision(2); // two places after the . int n = years * 12; // years to periods double r = apr / 12; // APR to periodic rate double payment = p * r / (1 - pow(1 + r, -n)); // periodic payment cout << "Monthly Payment: " << payment << endl; return 0; }
Compare the formula at the top of this page with the C++ code while paying particular attention to the parentheses in the code: The outer parentheses group the denominator sub-expressions together. In contrast, the inner parentheses form the pow function's two arguments. Notice that parentheses are not needed around 1+r, as the comma is sufficient to separate the two arguments, which the program evaluates separately before calling the pow function.
The C++ code also uses the '-' character in two places, but each use represents a different operator. The '-' operator in 1 - pow... is the binary subtraction operator, while the '-' operator in -n is the unary negation operator.