Program working wrong for big values

  • Thread starter preceptor1919
  • Start date
  • Tags
    Program
In summary, the program gives wrong results if the interest is high enough so that the money generated in a month is greater than what you withdraw.
  • #1
preceptor1919
35
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
  • #2
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
 
  • #3
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?
 
  • #4
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;
}
 
  • #5
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.
 
  • #6
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
 
  • #7
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
 
  • #8
Yah I also saw my mistake there haha thank you.
 
  • #9
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?
 

FAQ: Program working wrong for big values

1. Why does my program work correctly for small values, but not for big values?

There could be several reasons for this. One possibility is that your program is encountering an overflow or underflow error, which occurs when a number is too large or too small to be represented accurately by the computer. Another possibility is that your program is not designed to handle large values and may need to be optimized for efficiency.

2. How can I fix my program to work for big values?

First, check for any potential overflow or underflow errors in your code. You may need to use a different data type or adjust your calculations to handle larger values. Additionally, you may need to optimize your program for efficiency by using more efficient algorithms or data structures.

3. Is there a limit to the size of values that a program can handle?

Yes, there is a limit to the size of values that a program can handle. This limit is determined by the data type that is used to store the values. For example, the maximum value that can be stored in an integer variable is typically 2,147,483,647.

4. How can I prevent my program from crashing when encountering large values?

To prevent your program from crashing, you can use error handling techniques such as try-catch statements to catch any errors that may occur when dealing with large values. You can also use conditional statements to check for potential overflow or underflow errors and handle them appropriately.

5. Can I test my program with large values without actually inputting them?

Yes, you can test your program with large values by using test cases or sample data sets that contain large values. This will allow you to identify and fix any issues related to handling large values without having to input them manually.

Back
Top