Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNancy Melucci
Courses Plus Student 36,143 PointsLoan Payment formula in C#
Writing a windows form application in C#. I've been given this formula to use to write the code to calculate loan payments...
(loan amount x interest rate) /
(1 - (1 / (1 + interest rate))term * 12)
I've made it into the formula below. But my monthly payment is off by 16 cents per month (compared to the example formula) and my total payoff amount is off by a few hundred dollars...
Does anyone out there have an example of a version that is likely to be more accurate?
int term = yearsToPay * 12 * -1;
payAmount = (amount * interest)/(1 - (Math.Pow(1 + interest, term)));
2 Answers
James Churchill
Treehouse TeacherNancy,
I suspect that you're not calculating your rate of interest correctly, which is dependent on how often payments are to be made.
Here's a solution that I came up that seems to work accurately for monthly payments.
namespace LoanCalculator
{
class Program
{
static void Main(string[] args)
{
var loanAmount = 10000;
var interest = 5.0;
var numberOfYears = 5;
// rate of interest and number of payments for monthly payments
var rateOfInterest = interest / 1200;
var numberOfPayments = numberOfYears * 12;
// loan amount = (interest rate * loan amount) / (1 - (1 + interest rate)^(number of payments * -1))
var paymentAmount = (rateOfInterest * loanAmount) / (1 - Math.Pow(1 + rateOfInterest, numberOfPayments * -1));
Console.WriteLine(paymentAmount);
}
}
}
Thanks ~James
Nancy Melucci
Courses Plus Student 36,143 PointsThanks that worked. I am taking accounting but don't have much experience to this point with financial formulas.
I appreciate the help. I am looking forward to summer, when I plan to take more C# here at Treehouse.