How much time do i need to invest z(amount) : from compound interest

Hello everyone;
I am struggling with a simple calculation here.
Question:
Write a program that calculates how long it will take Mr Jones to get y amount of money giving that he puts x amount of money every year with a yearly interest rate of r.

This is what i have done so far but it doesn't give me what i want.

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()
{
    double r, x, y; //r is interest rate; x is savings amount & y is target amount

    cout << "How much are you ready to invest every year? ";
    cin >> x;

    cout << "How much is your final target amount? ";
    cin >> y;

    cout << "What is the interest rate? (please give 0.043 for 4.3%) ";
    cin >> r;


    //Calculate interest after year 1
    double z = x*r; //z is interest after every year
  
// cout << z << endl; // i tried this to test if z works and it does
    for (int i = x; i < y; i++)
    {
        x += z;
        cout << x << endl;
    }

    return 0;
}

//For some reason, this doesnt work. I know this must be trivial but it is obscured to me.  


Furthermore, i don't know how to incorporate time in this stuff. Time is the most important parameter in this work. Please help.

Thanks in advance
Last edited on
1
2
3
4
5
6
7
8
9
10
int years =0;
double total = 0;
while(total < y)
{
    total+=x;
    total+=total*r;
    years++;
    cout << "year: " <<  years << "    total: "<< total << endl;

}



closed account (48T7M4Gy)
.
Last edited on
The condition has to be: total < y
closed account (48T7M4Gy)
.
Last edited on
The loop needs to exit when total >= y, to ensure that the target is met. Therefore, it has to keep looping while total < y.
@ Kevin C & Kemort, i tried both total < y and total <= y and the results was the same.

Thanks very much Kemort for this work. But please, how did you reason this total = (total + x)*(1 + r)? Why (1+r)? Is it a known formula or just programming reasoning.

Please don't be angry but this problem has bothered me for a while before bringing it to the forum.
Last edited on
Corrected code looks like this
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
34
35
36
37
38
#include <iostream>
#include <cmath> 

using namespace std;


int main()
{
    //Variables
    double yearlyAmount, targetAmount, interestRate,endTime; 


    //Getting the information needed
    cout << "How much do you want to put in every year?: ";
    cin >> yearlyAmount;

    //Target amount
    cout << "What is your target amount?: ";
    cin >> targetAmount;

    //Give intereste
    cout << "Give the yearly interest  (0.024 for 2.4): ";
    cin >> interestRate;

   //Calculations
   double total = 0; // total is the final sum of money targeted. 

	
  for (int i = 0; total <= targetAmount; i++) //the loop ends when  total == targetAmount
  {
   total = total * (1 + interestRate) + yearlyAmount;
   cout << "After year " << i + 1 << "  you have  $" << total << endl;
  }


cin.get();
   return 0;
}
closed account (48T7M4Gy)
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
34
35
36
#include <iostream>

using namespace std;

int main()
{
    //Variables
    double yearlyAmount, targetAmount, interestRate,endTime; 


    //Getting the information needed
    cout << "How much do you want to put in every year?: ";
    cin >> yearlyAmount;

    //Target amount
    cout << "What is your target amount?: ";
    cin >> targetAmount;

    //Give intereste
    cout << "Give the yearly interest  (0.024 for 2.4): ";
    cin >> interestRate;

   //Calculations
   double total = 0; // total is the final sum of money targeted. 

	
  for (int i = 1; total <= targetAmount; i++) //the loop ends when  total == targetAmount
  {
   total = (total + yearlyAmount) * (1 + interestRate);
   cout << "At the end of year " << i << "  you have  $" << total << endl;
  }


cin.get();
   return 0;
}
How much do you want to put in every year?: 50
What is your target amount?: 500
Give the yearly interest  (0.024 for 2.4): .10
At the end of year 1  you have  $55
At the end of year 2  you have  $115.5
At the end of year 3  you have  $182.05
At the end of year 4  you have  $255.255
At the end of year 5  you have  $335.781
At the end of year 6  you have  $424.359
At the end of year 7  you have  $521.794
 
Exit code: 0 (normal program termination)


Assuming you put the money in at the start of the year, the program you have needs a small adjustment which I have made to take into account the first payment earning interest in the first year. So at 10% interest at the end of the first year you have 1.1 * (50) = $55.00, end of second year 1.10 * ( 55 + 50) = $115.50 etc. :)
Topic archived. No new replies allowed.