-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisting02.11.ComputeLoan.cpp
39 lines (30 loc) · 1.14 KB
/
listing02.11.ComputeLoan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// Enter yearly interest rate
cout << "Enter yearly interest rate, for example 8.25 : ";
double annualInterestRate;
cin >> annualInterestRate;
// Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
// Enter number of years
cout << "Enter number of years as an integer, for example 5: ";
int numberOfYears;
cin >> numberOfYears;
// Enter loan amount
cout << "Enter loan amount, for example 120000.95: ";
double loanAmount;
cin >> loanAmount;
// Calculate payment
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - 1 / pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
monthlyPayment = static_cast<int>(monthlyPayment * 100) / 100.0;
totalPayment = static_cast<int>(totalPayment * 100) / 100.0;
// Display result
cout << "The monthly payment is " << monthlyPayment << endl <<
"The total payment is " << totalPayment << endl;
return 0;
}