2.7.3. payment.cpp

Time: 00:04:20 | Download: Large, Large (CC), Small | Streaming, Streaming (CC)
Review

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.

\[payment = {PR \over {1 - (1 + R)^{-N}}} \]
Periodic loan payment. The formula for calculating the amount of a periodic loan payment. The period is equivalent to a month if the borrower makes twelve payments per year, one per month.

Test Cases

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

Formatting The Output

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);
A set of flags controls cout's formatting. Each is like a light switch - they are either on or off. The setf (short for set flags) function turns the switches on or off. With the default cout settings, large numbers are displayed using scientific notation, which may not look right when printing monetary values. The ios::fixed flag or switch reconfigures cout so that it always displays numbers in a fixed-point format (no scientific notation) regardless of the number's size.
cout.precision(2);
The function configures cout to print two digits after the decimal point, which works well for U.S. currency. The output is rounded as necessary.

Note that these two statements only need to run once; cout remains as configured until the program ends or other statements reconfigure it.

Program

/*
 * 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;
}
mort.cpp. The statement payment = p * r / (1 - pow(1 + r, -n)); is another example of converting a formula into C++ code. The conversion relies heavily on the pow function.

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.