Jump to content

Compound interest with annual addition

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hello all,

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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
change
amount = principal * pow((1 + rate), 10) + annual_contribution * (pow((1 + rate), 10) - 1) / rate;
to
principal = principal * (1 + rate) + annual_contribution ;

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users