C++ Question: Calculate the Investment at time n

  • Comp Sci
  • Thread starter Hughng
  • Start date
  • Tags
    C++ Time
In summary: 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
Hughng
26
0

Homework Statement


Pretend you have some money (ha!) that you want to invest in the stock market. Ask the user for:

  1. The initial investment
  2. Yearly estimated market growth
  3. How much (if any) you wish to withdraw from the account per year
  4. The desired level of money you wish to get
The growth formula per year is simply:
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:
Physics news on Phys.org
  • #2
Hughng said:

Homework Statement


Pretend you have some money (ha!) that you want to invest in the stock market. Ask the user for:

  1. The initial investment
  2. Yearly estimated market growth
  3. How much (if any) you wish to withdraw from the account per year
  4. The desired level of money you wish to get
The growth formula per year is simply:
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.
What were the other input values you used?

Also, when you post code, please include code tags. I have added them in your post.

They look like this:
[code=c]
<your code>
[/code]
Hughng said:

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;
}
 
  • #3
Here are the data:

Initial investment? 1000
Yearly growth rate? 0.1
Yearly withdraw amount? 20
Desired balance? 1000000
Years: 18
Balance at end: 0
Amount withdrawn over period: 340.114
 
  • #4
Hughng said:
Yearly growth rate? 0.1
You said in an earlier post that you were using -0.1 as the growth rate.
Which is it, a growth rate of 0.1 or -0.1?
 
  • #5
The growth rate is -0.1. I am sorry I did not notice. Thank you.
 
  • #6
If the growth rate is -0.1 and you start with 1000 and withdraw 20 each year, you'll never get to 1,000,000.
 
  • #8
Hello Mark,
Yes, if you keep withdrawing the money given the initial amount above, you will not get the desire investment. The assignment will ask you to "cout" O for final balance, but you still have to print out the number of years that will take to exhaust your account, and the total amount you can withdraw until you have nothing to withdraw. However, I had an issue which was I could not print out the desired withdrawn amount as the assignment require: 340.11. I got a whole number which is 360.
Thank you.
 
  • #9
Hughng said:
I had an issue which was I could not print out the desired withdrawn amount as the assignment require: 340.11. I got a whole number which is 360.
Why do you think that $340.11 is the correct amount? If you withdraw $20 per year for 18 years, that's $360.
 
  • #10
I did not think so, but the assignment asked me to print out the desired result. I submitted the assignment, but I really appreciate your help.
 

1. How do I calculate the investment at a specific time (n) in C++?

To calculate the investment at a specific time (n), you will need to use the formula: Investment at time n = Initial Investment * (1 + Interest Rate)^n. In C++, you can use the pow() function from the library to raise the interest rate to the power of n.

2. How do I input the initial investment and interest rate values in C++?

To input values in C++, you can use the cin function to prompt the user for input and the >> operator to store the input in a variable. For example, to input the initial investment, you can use: cin >> initial_investment; where "initial_investment" is the name of your variable. Repeat this process for the interest rate value.

3. How do I handle compound interest when calculating the investment at time n?

You can handle compound interest by using the formula: Investment at time n = Initial Investment * (1 + (Interest Rate / Compounding Frequency))^(Compounding Frequency * n). The compounding frequency refers to how often the interest is added to the investment (e.g. monthly, quarterly, yearly). In C++, you can use a loop to calculate the investment for each compounding period.

4. Can I calculate the investment at multiple time points (n) in C++?

Yes, you can calculate the investment at multiple time points in C++. You can use a loop to iterate through each time point and calculate the investment at that specific point using the formula mentioned in the first question. You can also store the values in an array or vector for later use.

5. How do I handle error handling in my C++ program when calculating the investment at time n?

To handle error handling in your C++ program, you can use conditional statements to check for any invalid inputs or values that may cause errors in your calculations. You can also use try-catch blocks to catch any potential errors and handle them accordingly. It is always a good practice to validate user input before performing any calculations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
Back
Top