Not understanding why my output is wrong

doing an online book class on C++ and up to the chapter about "while loops".
their is a sample problem that ask:

"Suppose we add a fixed amount of money into our bank account at the beginning of every year. Modify the program from this section to show how many years it takes for the balance to double, given the annual contributions and the interest. Also print the final balance."

the correct outputs according to the book is:
with an annual contribution of 100$:
Year: 13
Balance: 20527.8


the output i am getting with my program is:
Year: 13
Balance: 20627.8




i'm not sure what i am doing wrong, it seems according to the instructions that i loop in adding the contribution and interest amount to each years balance. my code is as follows:



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
#include <iostream>
using namespace std;

int main()
{  
   const double RATE = 5;
   const double INITIAL_BALANCE = 10000;
   const double TARGET = 2 * INITIAL_BALANCE;
   
   cout << "Annual contribution: " << endl;
   double contribution; 
   cin >> contribution;

   double balance = INITIAL_BALANCE;
   int year = 0;

   // TODO: Add annual contribution, but not in year 0
   while (balance <= TARGET)
   {
         

      year = year + 1;
     double  interest =  balance * 5/100;
     balance = balance + interest + contribution;
 
   }
   
   
   cout << "Year: " << year << endl;
   cout << "Balance: " << balance << endl;

   return 0;
}
Last edited on
I suspect the book's expected output is probably a typo.
Topic archived. No new replies allowed.