/* Name: save.cpp Date: 9/27/07 Author: Frank McCown This program calculates the amount of money saved given the monthly deposit, APR, and number of years. The deposit is added at the beginning of the month. Interest is compounded monthly. Sample Run: Enter monthly deposit: 250 Enter APR: 7 Enter number of years: 5 Year Total 1 3116.22 2 6457.71 3 10040.76 4 13882.82 5 18002.63 */ #include #include // For setw using namespace std; const int MONTHS_PER_YEAR = 12; void main() { int years, month, total_months; double total, deposit, apr, mpr; // Get initial values cout << "Enter monthly deposit: "; cin >> deposit; cout << "Enter APR: "; cin >> apr; cout << "Enter number of years: "; cin >> years; // Convert annual percentage rate to monthly // (12% APR = 1% MPR) mpr = apr / MONTHS_PER_YEAR * 0.01; cout << endl << setw(4) << "Year" << setw(10) << "Total" << endl; // Round all decimals to 2 places. cout.precision(2); // Put into normal notation (not scientific) // and always show decimal point, even if .00 cout.setf(ios::fixed | ios::showpoint); total_months = years * MONTHS_PER_YEAR; // Determine new total for every month month = 1; total = 0; while (month <= total_months) { total += (total + deposit) * mpr + deposit; // Only output yearly values, not monthly if (month % MONTHS_PER_YEAR == 0) cout << setw(4) << month / MONTHS_PER_YEAR << setw(10) << total << endl; month++; } }