Hello, I have been asked to create the following output using a loop:
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
Now, this is the code I have written:
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
|
// Enter your program here
#include <iostream>
using namespace std;
// Enter your program here
#include <iostream>
using namespace std;
int main(){
float balance = 0.0;
int month = 1;
float interest = 0.0;
float newBalance = 0.0;
float newInterest = 0.0;
cin >> balance;
cout << "How much do you want to save each month? " << balance << endl;
while (newBalance < 1000.00){
cout << "Month " << month << ": " << "$" << newBalance << " deposited " << balance << " interest earned is " << interest << endl;
month++;
newBalance = balance + newBalance;
interest = (0.01 * newBalance);
newBalance = newBalance + interest;
}
cout << "It took " << month << " months, and you now have $" << newBalance << endl;
return 0;
}
|
My output is this:
How much do you want to save each month? 100
Month 1: $0 deposited 100 interest earned is 0
Month 2: $101 deposited 100 interest earned is 1
Month 3: $203.01 deposited 100 interest earned is 2.01
Month 4: $306.04 deposited 100 interest earned is 3.0301
Month 5: $410.1 deposited 100 interest earned is 4.0604
Month 6: $515.201 deposited 100 interest earned is 5.10101
Month 7: $621.354 deposited 100 interest earned is 6.15201
Month 8: $728.567 deposited 100 interest earned is 7.21354
Month 9: $836.853 deposited 100 interest earned is 8.28567
Month 10: $946.221 deposited 100 interest earned is 9.36853
It took 11 months, and you now have $1056.68
The interest is correct I think, but there is something off about the looping sequence. I have been up all night trying to do this, I'm beginning to suspect I'm not very good at this. Please help.
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
|
// Enter your program here
#include <iostream>
using namespace std;
int main(){
float balance = 0.0;
int month = 1;
float interest = 0.0;
float newBalance = 0.0;
float newInterest = 0.0;
cin >> balance;
cout << "How much do you want to save each month? " << balance << endl;
while (newBalance < 1000.00){
cout << "Month " << month << ": " << "$" << newBalance << " deposited " << balance << " interest earned is " << interest << endl;
month++;
newBalance = balance + newBalance;
interest = (0.01 * newBalance);
newBalance = newBalance + interest;
}
cout << "It took " << month << " months, and you now have $" << newBalance << endl;
return 0;
}
|