To calculate number of years for an investment to double

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Diksha1310
    New Member
    • Sep 2015
    • 2

    To calculate number of years for an investment to double

    I tried it but i got the coding all wrong maybe.

    Write a program that uses a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double.
    Note: the amount of the initial investment does not matter; you can use USD 100.
    Here's my code..Please help me. I cant make out a proper while loop.

    int main()
    {
    float rate;
    int count=1;
    float Amount=0;
    float Interest=0;
    float investment=100;
    cout<<"Enter the % annual rate: ";
    cin>>rate;
    while(investmen t<=(2*investmen t)){
    Interest=((inve stment*rate*cou nt)/100);
    Amount=Interest +investment;
    if(investment== (2*investment)) {
    cout<<"The number of years it takes for an investment to double is: "<<count<<" years"<<endl;
    }
    count++;
    }
    return 0;
    }
  • Clearner321
    New Member
    • Sep 2015
    • 22

    #2
    In the while condition check I think you should check for amount less than 2 times the investment.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You are trying to use floating point in comparison operations. Rounding can throw these comparisons off. Especially equal/not equal. You should never use floating point in a financial application.

      I suggest you use int for your amounts. Keep values in pennies. The only time you need a decimal is when you display the amount.

      investment/100 gets the dollars.
      investment %100 gets the pennies.

      Code:
      cout << investment/100 << "." << investment %100 << endl;

      Comment

      Working...