I'm trying to write a simple compound interest console app. The interest rate is 4 percent, the starting principal is $1,000. And finally, the annual contribution is $100. I have to use a for loop, but my loop appears to be stuck and only displays the final value for all ten years. Is it the formula I'm using, or is my loop really messed up? I'm pretty sure it's something small and that's an easy fix. Any suggestions?
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
int year;
float annual_contribution = 100;
float amount;
float principal = 1000.00;
float rate = .04;
cout << "Year" << setw(21) << "With interest" << endl;
cout << fixed << setprecision(2);
for (year = 1; year <= 10; year++)
{
amount = principal * pow((1 + rate), 10) + annual_contribution * (pow((1 + rate), 10) - 1) / rate;
cout << setw(4) << year << setw(21) << amount << endl;
}
return 0;
}
It's coming out with the correct future value of $2680.85, but it lists $2680.85 for every year.


Sign In
Create Account


Back to top









