-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmortizationTable.cpp
139 lines (119 loc) · 4.6 KB
/
AmortizationTable.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Name: Donald Tran
// Date: 02/05/17
// AU User ID: DZT0021
// File Name: hw2.cpp
//
// Instructions to compile:
// g++ -std=c++11 hw2.cpp
//
// README:
// This program was created using the "CLion" IDE and
// compiled using CMake Version 3.6 and the g++ compiler on
// Tux056 via SSH Protocol
//
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main () {
double balanceAmt, annualRate, monthpAmt;
double monthRate, princpAmt, minimumpAmt;
double interest, totalInterest;
int monthCount = 0;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\nLoan Amount: ";
cin >> balanceAmt;
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entry Invalid. Enter a 'number' to try again.\n\n";
cout << "Loan Amount: ";
cin >> balanceAmt;
}
cout << "Interest Rate (% per year): ";
cin >> annualRate;
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entry Invalid. Enter a 'number' to try again.\n\n";
cout << "Interest Rate (% per year): ";
cin >> annualRate;
}
cout << "Monthly Payments: ";
cin >> monthpAmt;
while (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Entry Invalid. Enter a 'number' to try again.\n\n";
cout << "Monthly Payments:: ";
cin >> monthpAmt;
}
// Calculates the monthly interest rate which will be used
// to determine the amount of interest compounded each month
monthRate = (annualRate / 100) / 12;
minimumpAmt = balanceAmt * monthRate;
if (monthpAmt <= minimumpAmt) {
cout << "\n\nYour monthly minimum payment must be ";
cout << "greater than " << minimumpAmt << " in order to pay off this debt.\n";
cout << "Please try again later.\n\n";
return 0;
}
cout << "\n********************************************************************************\n";
cout << "\t\t\tAmortization Table\n";
cout << "********************************************************************************\n";
cout << left << setw(8) << setfill(' ') << "Month";
cout << left << setw(15) << setfill(' ') << "Balance";
cout << left << setw(15) << setfill(' ') << "Payment";
cout << left << setw(15) << setfill(' ') << "Rate";
cout << left << setw(15) << setfill(' ') << "Interest";
cout << left << setw(15) << setfill(' ') << "Principal" << endl;
cout << left << setw(8) << setfill(' ') << "0";
cout << "$";
cout << left << setw(14) << setfill(' ') << balanceAmt;
cout << left << setw(15) << setfill(' ') << "N/A";
cout << left << setw(15) << setfill(' ') << "N/A";
cout << left << setw(15) << setfill(' ') << "N/A";
cout << left << setw(15) << setfill(' ') << "N/A" << endl;
while (monthpAmt <= balanceAmt) {
interest = balanceAmt * monthRate;
princpAmt = monthpAmt - interest;
balanceAmt -= princpAmt;
totalInterest += interest;
++monthCount;
cout << left << setw(8) << setfill(' ') << monthCount;
cout << "$";
cout << left << setw(14) << setfill(' ') << balanceAmt;
cout << "$";
cout << left << setw(14) << setfill(' ') << monthpAmt;
cout << left << setw(15) << setfill(' ') << (monthRate * 100);
cout << "$";
cout << left << setw(14) << setfill(' ') << interest;
cout << "-$";
cout << left << setw(15) << setfill(' ') << princpAmt << endl;
}
if (monthpAmt > balanceAmt && balanceAmt > 0) {
monthpAmt = balanceAmt;
interest = monthpAmt * monthRate;
princpAmt = balanceAmt;
totalInterest += interest;
balanceAmt = 0;
++monthCount;
cout << left << setw(8) << setfill(' ') << monthCount;
cout << "$";
cout << left << setw(14) << setfill(' ') << balanceAmt;
cout << "$";
cout << left << setw(14) << setfill(' ') << monthpAmt;
cout << left << setw(15) << setfill(' ') << (monthRate * 100);
cout << "$";
cout << left << setw(14) << setfill(' ') << interest;
cout << "-$";
cout << left << setw(15) << setfill(' ') << princpAmt << endl;
}
cout << "********************************************************************************\n\n";
cout << "It takes " << monthCount << " months to pay off your loan.\n";
cout << "Total interest paid is: $" << totalInterest << endl;
return 0;
}