So for my C# programming class I have to calculate the number of months it would take to pay off a credit card balance given the balance, monthly payment and APR.
Here is the formula we were given to interpret:
Here is the C# version I made:
months = (double)decimal.Divide(-1, 30) * (
Math.Log(1 + (double)(balance / payment) * (1 - Math.Pow((1 + (apr / 365)), 30)))
/ Math.Log(1 + (apr / 365))
);
It works for every example I've tried, except one, which is specified in a question I need to have the answer for:
How many months will it take Alice to pay off a $7,500.00 balance if the APR value is 0.22 and she pays $125.00 per month?
The output of which looks like:
Enter the credit card balance (example: 10000.00)
7500
Enter the monthly payment amount (example: 250.00)
125
Enter the annual percentage rate (example: 0.15)
0.22
It will take NaN months to pay off the balance.
Goodbye! Press any key to exit.
In my investigations, I plugged the formula into WolframAlpha and it returns two different values (http://bit.ly/1LQSsEx) perhaps this has something to do with it, but I am new to C# and have reached the end of my knowledge.
What is happening here, and how do I fix it?