user10477513
user10477513

Reputation:

writing a code for time to double annual interest rate

I'm trying to write a code for annual interest rate that lets you enter any amount, and it will show you approximately how many years it takes for your money to at least double. The given interest rate is 5% yearly. Thing is, it's not working properly and it's displaying absurdly high numbers, like 200 years or so.

#include <iostream>
using namespace std;

int main() {
    int deposit;
    int counter;

    cout << "Deposit an amount NO LESS than 1000." << endl;
    cin >> deposit;

    for (deposit ;; deposit = 1.05 * deposit) {
        counter = counter+1;

        if (deposit >= 2 * deposit) {
            cout << endl;
            cout << "Your money will double in "<< counter <<" years." << endl;
            break;
        }
    }   
}

Upvotes: 0

Views: 468

Answers (5)

Jeff Foster
Jeff Foster

Reputation: 44706

Instead of using a loop, you could calculate the time taken to double the money directly.

The amount of money is not interesting, so you don't need to store the amount of money. It's only the rate of return that's interesting.

You can calculate it directly as log(2) / log(r) where r is the rate of return. For example log(2) / log(1.05) gives you the exact time to double an initial amount of money with a 5% return.

Include the standard <cmath> header to get std::log().

#include <iostream>
#include <cmath>

int main() {
  double yearsToDouble = std::log(2) / std::log(1.05);
  std::cout << "Your money will double in "<< yearsToDouble << " years." << std::endl;
}  

Upvotes: 4

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385274

To add to the other answers, which are largely correct in pointing out that deposit > 2*deposit can never be true (you need a second variable to record the initial value!), the only reason your loop ends at all is because deposit gets so large that 2*deposit "wraps around" due to overflow.

This appears to make 2*deposit bigger than deposit (logically impossible — you need to fix this comparison!) although strictly speaking the results are undefined.

Apparently this happens to you after 200 or so iterations.

As for suggestions to switch to a floating-point type like double, this is tempting, and may be sufficient in this simple case, but as a general rule you should avoid floating-point when you don't need it as it introduces complexities and inaccuracies for very little gain.

I would recommend counting in integer pennies, or tenths of pennies, instead. You can achieve it by multiplying the input by 100 or 1000. The resulting incremental multiplication by 1.05 will have a rounding factor, then, but this is what the banks will be doing too!

Upvotes: 2

Shravan40
Shravan40

Reputation: 9908

if (deposit >= 2 * deposit) {
        cout << endl;
        cout << "Your money will double in "<< counter <<" years." << endl;
        break;
    }

In the above if statement you are expecting deposit to be greater than or equal to 2 times of deposit. Which can only be true in case if the value of deposit is zero or less than zero.

I will suggest you to use a temp variable to keep the input value of deposit and proceed.

Upvotes: 2

Blaze
Blaze

Reputation: 16876

This line

if (deposit >= 2 * deposit) {

Will not evaluate to true (unless deposit is negative or barring someedge case). You probably wanted to compare it to an initial value. So after this:

cin >> deposit;

I would put

double initialDeposit = deposit;

And then change the other line to

if (deposit >= 2 * initialDeposit) {

Upvotes: 1

P.W
P.W

Reputation: 26800

Use a variable to store the initial deposit so that it can be compared to the cumulative amount with interest.

for (float initdeposit = deposit;; deposit = 1.05 * deposit)
{
    counter = counter+1;

    if (deposit >= 2 * initdeposit)
    {
        cout << endl;
        cout << "Your money will double in "<< counter <<" years." << endl;
        break;
    }
}   

a.exe

Deposit an amount NO LESS than 1000.
1000

Your money will double in 16 years.

Note: No matter what the amount is, the time taken to double will be the same always. :)

Upvotes: 2

Related Questions