Program working wrong for big values

  • Thread starter Thread starter preceptor1919
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around a programming issue related to calculating how long it takes for a bank account to be depleted with monthly withdrawals and a 6% annual interest rate. The user is experiencing incorrect results for larger values, specifically noting that a balance of 100,000 yields an unrealistic duration of 526 years before depletion. The key point raised is that if the monthly interest generated is equal to or greater than the monthly withdrawal amount, the account will never be depleted. The user’s code correctly handles smaller balances but fails at larger ones, prompting a suggestion to calculate interest separately and display monthly balances for clarity. The conversation highlights that with a 100,000 balance, the monthly interest of 500 offsets the monthly withdrawal, leading to confusion about the account's depletion. A mathematical solution is mentioned, indicating that if the initial amount is greater than or equal to the monthly withdrawal divided by the monthly interest, the account can sustain withdrawals indefinitely.
preceptor1919
Messages
35
Reaction score
0
I am trying to figure out why my program gives wrong results or not execute for big values. I am trying to compute how long before a bank account is depleted if it has an interest and 500 is withdrawn from it monthly. If I use 100000, it gives a value of 526years, which I think is wrong. And if you put 150000. It takes so long for it to finish executing.
 
Technology news on Phys.org
Hhhmmm...well, if the interest is high enough so that the money generated in a month is greater than what you withdraw you will never finish the money
 
The interest is 6%

So if my program works fine for small valued like 500,10k and the likes,should it be just fine for big values?
 
Is it alright if I post the piece of my code that I use to extract how many months will pass?

while (iBalance >= 500){
iBalance = iBalance * interestMultiplier;//interestMultiplier = (1 + 0.06/12) Getting the amount added because of the interest rate

iBalance = iBalance - 500; //Withdraw 500 every month from the remaining balance

months += 1; //Increment 1 for each month's withdrawal
}



if (iBalance < 500 && !(iBalance == 0)){ //Last Withdrawal if balance is less than 500 but not 0
iBalance = iBalance - iBalance;

months+=1;
}
 
Try calculating the interest added as a separate variable. Then for each month display the interest, and the final balance for that month. That way you can see whether the numbers look reasonable.
 
wow thanks. It showed that it worked until 99,999. I don't know what happened with 100,000. It seemed liked nothing is being withdrawn from it. I'll look into it. Thankyou
 
preceptor1919 said:
I don't know what happened with 100,000

The annual interest is 6% (0.06), so the monthly interest is 0.5% (0.005). Therefore, on a balance of 100,000, you get 500 in interest. Now you withdraw 500. What do you end up with? :D
 
Yah I also saw my mistake there haha thank you.
 
Just for the record, this problem has a mathematical solution. If $$ \mathrm{initial \ amount} \ge {\mathrm{monthly \ withdrawal} \over \mathrm{monthly \ interest}}, $$ you can withdraw infinitely. Can you show that?
 
Back
Top