-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterest.cpp
More file actions
57 lines (47 loc) · 1.82 KB
/
Interest.cpp
File metadata and controls
57 lines (47 loc) · 1.82 KB
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
// 12191706 ±èÁ¤Áø
// Chapter 10 - 10.12
// Compound-interest calculations with class DollarAmount and integers.
#include <iostream>
#include <iomanip>
#include <string>
#include "DollarAmount.h"
using namespace std;
int main() {
DollarAmount d1{ 123, 45 }; // $123.45
DollarAmount d2{ 15, 76 }; // $15.76
cout << "After adding d2 (" << d2.toString() << ") into d1 ("
<< d1.toString() << "), d1 = ";
d1 = d1 + d2; // modifies object d1
cout << d1.toString() << "\n";
cout << "After subtracting d2 (" << d2.toString() << ") from d1 ("
<< d1.toString() << "), d1 = ";
d1 = d1 - d2; // modifies object d1
cout << d1.toString() << "\n";
cout << "After dividing d2 (" << d2.toString() << ") from d1 ("
<< d1.toString() << "), d1 = ";
d1 = ((d1 / d2) * 100); // modifies object d1
cout << d1.toString() << "\n";
cout << "After multiplying d2 (" << d2.toString() << ") into d1 ("
<< d1.toString() << "), d1 = ";
d1 = (d1 * d2) / 100; // modifies object d1
cout << d1.toString() << "\n\n";
cout << "Enter integer interest rate and divisor. For example:\n"
<< "for 2%, enter: 2 100\n"
<< "for 2.3%, enter: 23 1000\n"
<< "for 2.37%, enter: 237 10000\n"
<< "for 2.375%, enter: 2375 100000\n> ";
int rate; // whole - number interest rate
int divisor; // divisor for rate
cin >> rate >> divisor;
DollarAmount balance{ 1000, 0 }; // initial principal amount in pennies
cout << "\nInitial balance: " << balance.toString() << endl;
// display headers
cout << "\nYear" << setw(20) << "Amount on deposit" << endl;
// calcurate amount on deposit for each of ten years
for (unsigned int year{ 1 }; year <= 10; year++) {
// increase balance by rate % (i.e., rate / divisor)
balance.addInterest(rate, divisor);
// display the year and the amount;
cout << setw(4) << year << setw(20) << balance.toString() << endl;
}
}