need in C++ and not using DO loop. Needs to be all in one program! Ask the user
ID: 3862440 • Letter: N
Question
need in C++ and not using DO loop. Needs to be all in one program!
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. For example:
How much do you want to save each month? 90 Month 1: $0 deposited 90 interest earned is 0.9 Month 2: $90.9 deposited 90 interest earned is 1.809 Month 3: $182.709 deposited 90 interest earned is 2.72709 Month 4: $275.436 deposited 90 interest earned is 3.65436 Month 5: $369.09 deposited 90 interest earned is 4.5909 Month 6: $463.681 deposited 90 interest earned is 5.53681 Month 7: $559.218 deposited 90 interest earned is 6.49218 Month 8: $655.71 deposited 90 interest earned is 7.4571 Month 9: $753.167 deposited 90 interest earned is 8.43167 Month 10: $851.599 deposited 90 interest earned is 9.41599 Month 11: $951.015 deposited 90 interest earned is 10.4102 It took 11 months, and you now have $1051.43
Explanation / Answer
#include <iostream>
using namespace std;
void doCalc(int amount, int month, double total,int count){
double interest;
if(total >= 1000){
count++;
interest = (total * 1)/100.0;
total = total + interest;
cout<<"It took "<<count<<" months, and you now have $ "<<total<<endl;
return;
}
else{
if(month == 1) {
total = amount;
interest = (total * 1)/100.0;
cout<<"Month "<<month<<": $0 deposited "<<amount<<" interest earned is "<<interest<<endl;
}
else{
interest = (total * 1)/100.0;
cout<<"Month "<<month<<": $"<<total<<" deposited 100 interest earned is "<<interest<<endl;
}
total = total + interest;
doCalc(amount, month+1, total+amount, count+1);
}
}
int main()
{
int amount;
int count = 0;
int month = 1;
double total = 0;
cout << "How much do you want to save each month? ";
cin >> amount;
doCalc(amount, month, total, count);
return 0;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
How much do you want to save each month? 100
Month 1: $0 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.101 deposited 100 interest earned is 5.10101
Month 6: $615.202 deposited 100 interest earned is 6.15202
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
It took 10 months, and you now have $ 1056.68
sh-4.2$ main
How much do you want to save each month? 90
Month 1: $0 deposited 90 interest earned is 0.9
Month 2: $180.9 deposited 100 interest earned is 1.809
Month 3: $272.709 deposited 100 interest earned is 2.72709
Month 4: $365.436 deposited 100 interest earned is 3.65436
Month 5: $459.09 deposited 100 interest earned is 4.5909
Month 6: $553.681 deposited 100 interest earned is 5.53681
Month 7: $649.218 deposited 100 interest earned is 6.49218
Month 8: $745.71 deposited 100 interest earned is 7.4571
Month 9: $843.167 deposited 100 interest earned is 8.43167
Month 10: $941.599 deposited 100 interest earned is 9.41599
It took 11 months, and you now have $ 1051.43
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.