For this homework you will write a program to solve a problem from Math 104. You do not need a knowledge of the mathematics as I have provided a fairly detailed algorithm to solve the problem. If you get in trouble, you can look at the code segments associated with the given step. In the end, my code is provided in case you run into difficulties.
I understand that you can just submit my code as your final result, but I feel that this is a huge mistake. Please work through the exercise.
A mortgage is a loan to purchase a home. You may take out multiple mortgages in your lifetime and if you do, one of these will most likely be the largest amount of money that you borrow.
It is common in Math 104 to solve mortgage problems. For these problems you are given
The price of the house.
The percent down payment.
The interest rate.
The number of years financed.
You are asked to compute
The down payment
The amount financed
The monthly payment
The total interest paid.
In addition, some of these problems ask if the borrower qualifies for the loan. This is based on
The borrower's monthly salary
The borrower's monthly payments of loans lasting more than 12 months.
The amount financed.
In this exercise, we will step through writing a program to compute these values.
Here is a typical problem from A Survey Of Mathematics with Applications by Angle, Abbot and Runde 10th.
Number 22 page 616 Evaluating a Lone Request:
Kathy wants to buy a condominium selling for $95,000. The taxes on the
property are $1500 per year, and homeowners' insurance is $330 per year.
Kathy's gross monthly income is $4000. She has 15 monthly payments of
$135 remaining on her van. The bank is requiring 20% down and is charging
9.5% interest rate. Her bank will approve a loan that has a total monthly
mortgage payment of principal, interest, property taxes and homeowners'
insurance that is less than or equal to 28% of her adjusted monthly income.
Determine the required down payment. $19,000
Determine 28% of her adjusted monthly income. $1,982.29
Determine a monthly payment of principal and interest for a 25-year loan. $664.04
Determine her total monthly payment including homeowners' insurance and taxes. $817.01
Does Kathy qualify for the loan? Yes
The only formula you will need is:
$$
m = \frac{p(\frac{r}{n})}{1-(1+\frac{r}{n})^{-nt}}
$$
Where:
m is the monthly payment
p is the amount financed
r is the annual interest rate
n is the number of payments per year
t is the number of years.
The Program
Begin by starting the program. Call it hw5.cpp
Build the basic shell.
Include the following libraries
cmath - we will need pow and round.
iomanip - we will need setw
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
return 0;
}
Basic Input and Down Payment
I tend to build my programs one part at a time. Ideally we should use functions but we don't know those yet so we will do this.
This type of computation will fit into a float, so use that data type.
Declare variables for priceOfHouse, percentDown, downPayment and amountFinanced.
Ask the user for the price of the house and store this.
Ask the user for the percent down and store this.
Compute the down payment by multiplying the price by the percent down. You will need to divide percent down by 100.
Print these in a nicely labeled output.
Your output should look something like
Enter the price of the house: 95000
Enter the percent down payment: 20
Price of House: $95000.00
Down Payment: $19000.00
Amount Financed: $76000.00
int main() {
float priceOfHouse{0},
percentDown{0},
downPayment{0},
amountFinanced{0};
// get loan information.
cout << "Enter the price of the house: ";
cin >> priceOfHouse;
cout << "Enter the percent down payment: ";
cin >> percentDown;
// compute the down payment.
downPayment = priceOfHouse * percentDown / 100.0;
amountFinanced = priceOfHouse - downPayment;
cout << endl;
// set up the output stream
cout << fixed;
cout << setprecision(2);
// Print basic financing information
cout << "Price of House: $" << priceOfHouse << endl;
cout << "Down Payment: $" << downPayment << endl;
cout << "Amount Financed: $" << amountFinanced << endl;
Compute the basic loan information
We will need get the terms of the loan.
Annual interest rate: interestRate
Number of years: years
Payments per year: paymentsPerYear
And compute the monthly payment (based on the amount financed, not the price of the house).
I used a temporary variable monthlyRate for $\frac{r}{n}$
Enter the price of the house: 95000
Enter the percent down payment: 20
Enter the annual interest rate: 9.5
Enter the number of years: 25
Enter the payments per year: 12
Price of House: $95000.00
Down Payment: $19000.00
Amount Financed: $76000.00
At 9.50% for 25 years.
With 12 payments per year.
The monthly payment is $664.01
...
float interestRate{0},
years{30},
paymentsPerYear{12};
float monthlyRate{0};
float monthlyPayment{0};
...
cout << "Enter the annual interest rate: ";
cin >> interestRate;
cout << "Enter the number of years: ";
cin >> years;
cout << "Enter the payments per year: ";
cin >> paymentsPerYear;
...
// compute the monthly payment.
monthlyRate = interestRate/paymentsPerYear/100.0;
monthlyPayment = (amountFinanced * monthlyRate)
/ (1-pow(1+monthlyRate,-years*paymentsPerYear ));
...
// print terms of loan.
cout << noshowpoint;
cout << "At " << interestRate << "%";
cout << setprecision(0) << " for " << years << " years." << endl;
cout << "With " << paymentsPerYear << " payments per year." << endl;
cout << setprecision(2) << showpoint;
cout << "The monthly payment is $" << monthlyPayment << endl;
Compute total monthly payment
For this we will need
Annual taxes: annualTaxes
Annual Insurace: insurance
Get each of these and divide by monthlyPayments
Add these to the monthly payment to get the total monthly payment.
And print this information.
Enter the price of the house: 95000
Enter the percent down payment: 20
Enter the annual interest rate: 9.5
Enter the number of years: 25
Enter the payments per year: 12
Enter the annual taxes: 1500
Enter the annual insurance cost: 336
Price of House: $95000.00
Down Payment: $19000.00
Amount Financed: $76000.00
At 9.50% for 25 years.
With 12 payments per year.
The monthly payment is $664.01
With annual taxes of $1500.00
and yearly insurance of $336.00
The total monthly payment is $817.01
...
float annualTaxes{0},
insurance{0};
float totalMonthlyPayment{0};
...
cout << "Enter the annual taxes: ";
cin >> annualTaxes;
cout << "Enter the annual insurance cost: ";
cin >> insurance;
cout << endl;
...
// compute the total monthly payment
totalMonthlyPayment = monthlyPayment + annualTaxes/paymentsPerYear
+ insurance/paymentsPerYear;
...
// print the total monthly payment
cout << "With annual taxes of $" << annualTaxes << endl;
cout << "and yearly insurance of $" << insurance << endl;
cout << "The total monthly payment is $" << totalMonthlyPayment << endl;
Can she afford the loan
For this we need:
Monthly income: monthlyIncome
Current payments: currentPayments
Subtract the current payments from the monthly income
Multiply the results by .28
If this is larger than the total monthly payment she qualifies for the loan.
Enter the price of the house: 95000
Enter the percent down payment: 20
Enter the annual interest rate: 9.5
Enter the number of years: 25
Enter the payments per year: 12
Enter the annual taxes: 1500
Enter the annual insurance cost: 336
Enter the monthly income: 4000
Enter the total of current monthly payments: 135
Price of House: $95000.00
Down Payment: $19000.00
Amount Financed: $76000.00
At 9.50% for 25 years.
With 12 payments per year.
The monthly payment is $664.01
With annual taxes of $1500.00
and yearly insurance of $336.00
The total monthly payment is $817.01
With a monthly income of $4000.00
and payments of $135.00
The adjusted monthly income is $1082.20
You qualify for this loan
const float BANK_PERCENT_LIMIT = 0.28;
...
float monthlyIncome {0},
currentPayments{0};
float adjustedIncome{0};
...
cout << "Enter the monthly income: ";
cin >> monthlyIncome;
cout << "Enter the total of current monthly payments: ";
cin >> currentPayments;
...
// compute the adjusted monthly income
adjustedIncome = (monthlyIncome - currentPayments) * BANK_PERCENT_LIMIT;
...
// print qualification information
cout << "With a monthly income of $" << monthlyIncome << endl;
cout << "and payments of $" << currentPayments << endl;
cout << "The adjusted monthly income is $" << adjustedIncome << endl;
if (adjustedIncome > totalMonthlyPayment) {
cout << "You qualify for this loan" << endl;
} else {
cout << "You do not qualify for this loan" << endl;
}
cout << endl;