Reputation: 9
Hey I am new to coding and was wondering if you guys could help me out with calculating different interest rates and then adding them to the next interest rate. So basically I'm trying to get interest rate A and adding it to the starting value of 100. then I want to get interest rate B for 100 and add that value to interest A. So far here is my code but I get 10 lines for each interest rate. Sorry if this sounds confusing but hopefully the code makes it more clear or maybe I could try to explain better if whoever reads this wants to. Thanks!!
int intv;
cout << " Accumulate interest on a savings account. ";
cout << " Starting value is $100 and interest rate is 1.25% ";
cout << endl;
intv = 100;
index = 1;
while ( index <= 10 )
{
cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.27% for a total of " << .0127 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.28% for a total of " << .0128 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.30% for a total of " << .0130 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.31% for a total of " << .0131 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.32% for a total of " << .0132 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.35% for a total of " << .0135 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.36% for a total of " << .0136 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.38% for a total of " << .0138 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.40% for a total of " << .0140 * intv + intv << "." << endl;
index = index + 1;
}
Instead of doing this for me I just want hints. I wanna fix this myself but am stuck as to what I have to do.
The desired out come of this is for the program to give me this:
Year 1 adds 1.25 for a total of 101.25 year 2 adds 1.27 for a total of 102.52 year 3 adds 1.28 for a total of 103.80 year 4 adds 1.30 for a total of 105.09 year 5 adds 1.31 for a total of 106.41 year 6 adds 1.33 for a total of 107.74 year 7 adds 1.35 for a total of 109.09 year 8 adds 1.36 for a total of 110.45 year 9 adds 1.38 for a total of 111.83 year 10 adds 1.40 for a total of 113.23
Total interest credited was 13.23
Upvotes: 0
Views: 170
Reputation: 736
You need use a function to replace
cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
The function can convert the index to adds value like
double foo(int index);
The input value is 'index', the output value is adds value like 1.25%, 1.38% .etc.
Then delete all cout lines. And just add this line:
cout << " Year " << index << " adds " << foo(index) * 100.0 << "% for a total of " << foo(index) * intv + intv << "." << endl;
I guess that is you want.
Upvotes: 0
Reputation: 57753
Sounds like you could use a for
loop:
double rate = 0.125;
for (unsigned int index = 0; index < max_rates; ++index)
{
cout << " Year " << index << " adds "
<< (rate * 100.0)
<< "% for a total of "
<< rate * intv + intv << "." << endl;
rate += 0.002;
}
Upvotes: 1