### Compound interest in for loop trouble

PLESE DELETE
Last edited on
`balance += annualDeposit * yearsToInvest;`

Looks like your code puts the total amount in at the start of the first year, instead of 500 in the first year and then 500 in the second.
NOTHING
Last edited on
maybe my calculations are wrong?
come on guys I need help :/ I need to solve this by tomorrow.
??
I get this as my ouput without adding in any values:

Output:
 ```Enter deposit amount: Another (y or n)? Total deposits: 0 Number of deposits: 1 Average deposit: 0 You want to invest in a new account that automatically deposits this average amount once each year. The money earns interest, compounded annually, and you want to know how much interest will be earned and the final balance. Enter number of years to invest: Enter annual interest rate (e.g. 8.5): Number of years invested: 134541077 Interest Rate: 4.00027e-34 Amount Invested: 0 Interest Earned: 0 Final Balance: 0```

Seems like there might be a problem with number of deposits?
Try changing everything to a double data type?
Last edited on
wow thats interesting. Had no idea. Wish I knew how to source it out. I spent so much time on this, I dont know where to look. Everything seem to make sense until the FOR loop.

finally got it :D
Last edited on
OP for reference.

Ive been working on this program for over 12 hours and I've been stuck on this compound interest part for over 4 hours. I made an account just to get some feedback on this. But im sure I will be on this forum a lot more.
Well here's the code.

The Problem is in PART 2 in the for loop.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114`` ``````#include #include #include using namespace std; int main() { //Input Variables char userResponse; float depositAmount; float totalDeposits; int depositCount; float averageDeposit; // Accumulators depositCount = 0; totalDeposits = 0; do { cout << "Enter deposit amount: "; cin >> depositAmount; totalDeposits =+ depositAmount + totalDeposits; depositCount++; cout << "Another (y or n)? "; cin >> userResponse; }while (userResponse == 'Y' || userResponse == 'y' ); //calculations averageDeposit = totalDeposits / depositCount; //output cout << endl; cout << "Total deposits: " << totalDeposits << endl; cout << "Number of deposits: " << depositCount << endl; cout << "Average deposit: " << averageDeposit << endl << endl; //PART 2 // Message about investing in new interest bearing acount. cout << "You want to invest in a new account that automatically deposits this average" << endl; cout << "amount once each year. The money earns interest, compounded annually, and" << endl; cout << "you want to know how much interest will be earned and the final balance." << endl; // Declare Variables float interestRate; int yearsToInvest; float annualDeposit; float balance; float amountInvested; float interestEarned; // Accumulator / Pre-calculations balance = 0; annualDeposit = averageDeposit; cout << endl; cout << "Enter number of years to invest: "; cin >> yearsToInvest; cout << endl; cout << "Enter annual interest rate (e.g. 8.5): "; cin >> interestRate; cout << endl; for (int yearCounter = 1; yearCounter <= yearsToInvest; yearCounter++); { balance += annualDeposit * yearsToInvest; balance = balance * pow(1 + interestRate/100, yearsToInvest) ; } //Calculations amountInvested = annualDeposit * yearsToInvest; interestEarned = balance - amountInvested; //Output cout << endl; cout << "Number of years invested: " << yearsToInvest << endl; cout << "Interest Rate: " << interestRate << endl; cout << "Amount Invested: " << amountInvested << endl; cout << "Interest Earned: " << interestEarned << endl; cout << "Final Balance: " << balance << endl; return 0; }``````

The program runs fine when I'm using only '1' for number of years invested and deposits.. However, when I use 2 or more years for number of years invested, the program doesn't do the math correctly.

Enter deposit amount: 500
Another (y or n)? n

Total deposits: 500
Number of deposits: 1
Average deposit: 500

You want to invest in a new account that automatically deposits this average
amount once each year. The money earns interest, compounded annually, and
you want to know how much interest will be earned and the final balance.

Enter number of years to invest: 2

Enter annual interest rate (e.g. 8.5): 8.5

Number of years invested: 2
Interest Rate: 8.5
Amount Invested: 1000
Interest Earned: 177.225
Final Balance: 1177.23
Press any key to continue . . .

FINAL BALANCE SHOULD BE 1131.11!
Topic archived. No new replies allowed.