Here's my mortgage length calculator:
Spoiler:
I'm sure there's a way to do it without a loop, but can't think of it atm.
Assuming £100,000 with a 4.75% rate and paying off £700/month
Code:
float mortgage = 100000f;
float rate = 4.75f; //%
int repaymentAmount = 700;
float months = 0;
while (mortgage > 0)
{
mortgage *= (1 + ((rate / 12) / 100));
mortgage -= repaymentAmount;
months++;
}
Console.WriteLine("Repayment duration: " + months + " months - " + (int)(months/12) + " Years and " + (months%12) + " months.");
Result:
Repayment duration: 211 months - 17 Years and 7 months.