- #1
Hughng
- 26
- 0
Homework Statement
Pretend you have some money (ha!) that you want to invest in the stock market. Ask the user for:
- The initial investment
- Yearly estimated market growth
- How much (if any) you wish to withdraw from the account per year
- The desired level of money you wish to get
I_new = I_old x (1+g)
Assume you withdraw money from the account after it grows (you cannot withdraw more than the account has!). Find out how long it takes to reach the desired amount of money (if it is reachable at all).
Homework Equations
I completed the program but when I ran g =-0.1 with other given values, I was supposed to obtain 340.11 as my instructor required, but I got a whole number: 360.
The Attempt at a Solution
C:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double I_0, g, W, BAL, I;
cout << " Initial Investment? ";
cin >> I_0;
cout << " Yearly growth rate? ";
cin >> g;
cout << " Yearly withdraw amount? ";
cin >> W;
cout << " Desired balance? ";
cin >> BAL;
I = I_0*(1 + g);
int i = 1;
if (W <= I_0 && g >= 0)
{
for (i = 1; I < BAL; i++)
{
I = I - W;
I = I*(1 + g);
}
cout << " Years: " << i << endl;
cout << " Balance at the end: "<< I << endl;
cout << " Amount withdrawn over period: " << W*i << endl;
}
else if (W >= I_0)
{
cout << " Years: " << i << endl;
cout << " Balance at the end: "<< 0 << endl;
cout << " Amount withdrawn over period: " << I << endl;
}
else if (W <= I_0 && g <= 0)
{
for (i = 1; I >= 0; i++)
{
I = I - W;
I = I*(1 + g);
}
cout << " Years: " << ceil(i) << endl;
cout << " Balance at the end: "<< 0 << endl;
cout << " Amount withdrawn over period: " <<W*i << endl;
}
return 0;
}
Last edited by a moderator: