I have included the code I have started along with the output and output expected. My first month keeps starting with my deposit amount of $100 when it is supposed to start with $0. If I do anything with the totAmount = totAmount + deposit line the program keeps timing out. At this point, I am unsure where to go with it. Any assistance is greatly appreciated.
Problem: Ask the user for an amount to save each month. Assuming a 1% per month interest rate, how many MONTHS will it take the user to save $1000. Add the savings at the END of each month.
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
|
#include <iostream>
using namespace std;
int main () {
float deposit = 0;
float totAmount = 0;
int months = 0;
float savings = 0;
cin >> deposit;
cout << "How much do you want to save each month? " << deposit << endl;
while( totAmount < 1000) {
++months;
cout << "Month " << months;
totAmount = totAmount + deposit;
cout << ": $" << totAmount;
cout << " deposited " << deposit;
cout << " interest earned is ";
cout << (totAmount * 0.01) << endl;
totAmount = totAmount + (totAmount * 0.01);
}
cout << "It took " << months << " months, and you now have $";
cout << totAmount << endl;
return 0;
}
|
Output:
How much do you want to save each month? 100
Month 1: $100 deposited 100 interest earned is 1
Month 2: $201 deposited 100 interest earned is 2.01
Month 3: $303.01 deposited 100 interest earned is 3.0301
Month 4: $406.04 deposited 100 interest earned is 4.0604
Month 5: $510.1 deposited 100 interest earned is 5.101
Month 6: $615.201 deposited 100 interest earned is 6.15201
Month 7: $721.354 deposited 100 interest earned is 7.21354
Month 8: $828.567 deposited 100 interest earned is 8.28567
Month 9: $936.853 deposited 100 interest earned is 9.36853
Month 10: $1046.22 deposited 100 interest earned is 10.4622
It took 10 months, and you now have $1056.68
Expected Output:
How much do you want to save each month? 100
Month 1: $0 deposited 100 interest earned is 1
Month 2: $101 deposited 100 interest earned is 2.01
Month 3: $203.01 deposited 100 interest earned is 3.0301
Month 4: $306.04 deposited 100 interest earned is 4.0604
Month 5: $410.101 deposited 100 interest earned is 5.10101
Month 6: $515.202 deposited 100 interest earned is 6.15202
Month 7: $621.354 deposited 100 interest earned is 7.21354
Month 8: $728.567 deposited 100 interest earned is 8.28567
Month 9: $836.853 deposited 100 interest earned is 9.36853
Month 10: $946.221 deposited 100 interest earned is 10.4622
It took 10 months, and you now have $1056.68