Title: Calculate interest for a loan in C#
This example uses three different methods for calculating interest on a loan. Enter the start and end dates for the loan, the annual interest rate, and the initial principle. When you click the Calculate button, the program calculates the loan's simple interest, interest compounded daily, and interest compounded continuously.
Simple Interest
To calculate the simple interest, the program uses the following formula.
Where:
 I = interest
 P = initial principle
 r = interest rate per period (per day)
 n = number of periods
This basically just applies the annual interest rate adjusted for the number of days of the loan. For example, if the loan lasts for half of a year, then the equation applies the annual interest rate times 1/2.
Compound Interest
To compound interest daily, you use the simple interest formula on each day of the loan. You then add the interest for that day to the loan's balance so during the later days you calculate interest on the interest. Over time the extra interest adds up, although for short duration loans the difference isn't too big.
Instead of calculating the interest for every day of the loan separately, you can use the following formula.
Where:
 A = total amount at the end
 P = initial principle
 r = interest rate per period (per day)
 n = number of periods
This basically just multiplies the initial principle by (1 + r) once for each compounding period.
Continuously Compounded Interest
The preceding formula compounds interest daily. You could compound the interest twice a day, hourly, every minute, or even more often. That means the interest is added to the balance more often so there is more interest on the interest. If you write out the formulas and take their limit as the length of the compounding period approaches zero, you get continuously compounded interest. It's as if you were calculating compound interest with an infinitely short compounding period.
The following formula calculates continuously compounded interest.
Where:
 A = total amount at the end
 P = initial principle
 e = the natural logarithm base (approximately 2.7183)
 r = interest rate per period (per day)
 n = number of periods
The Code
The following code shows how the program performs its calculations.
private void btnCalculate_Click(object sender, EventArgs e)
{
// Get inputs.
DateTime start_date = DateTime.Parse(txtStartDate.Text);
DateTime end_date = DateTime.Parse(txtEndDate.Text);
string ann_interest_string = txtAnnualPercent.Text;
if (ann_interest_string.Contains("%"))
ann_interest_string = ann_interest_string.Replace("%", "");
decimal annual_rate = decimal.Parse(ann_interest_string);
if (annual_rate >= 0.5m)
annual_rate /= 100m;
decimal principle =
decimal.Parse(txtPrinciple.Text, NumberStyles.Any);
// Redisplay the inputs to make sure they're correct.
txtStartDate.Text = start_date.ToShortDateString();
txtEndDate.Text = end_date.ToShortDateString();
txtAnnualPercent.Text = annual_rate.ToString("P");
txtPrinciple.Text = principle.ToString("C");
// Calculate the number of days in a "year."
// Calculate simple interest.
TimeSpan elapsed = end_date  start_date;
int days = (int)elapsed.TotalDays;
decimal daily_rate = annual_rate / 365m ;
decimal simple_interest = days * daily_rate * principle;
decimal simple_total = principle + simple_interest;
// Calculate interest compounded daily.
decimal daily_total = principle *
(decimal)Math.Pow(1 + (double)daily_rate, days);
decimal daily_interest = daily_total  principle;
// Calculate interest compounded continuously.
decimal continuous_total = principle *
(decimal)Math.Pow(Math.E, (double)daily_rate * days);
decimal continuous_interest = continuous_total  principle;
// Display results.
Console.WriteLine("# Days: " + days.ToString());
txtSimple.Text = simple_interest.ToString("C");
txtSimpleTotal.Text = simple_total.ToString("C");
txtDaily.Text = daily_interest.ToString("C");
txtDailyTotal.Text = daily_total.ToString("C");
txtContinuously.Text = continuous_interest.ToString("C");
txtContinuousTotal.Text = continuous_total.ToString("C");
}
The code gets the user's inputs, calculates interest using the three different methods, and then displays the results.
Notice in the picture at the top of the post that the simple interest is the smallest, the interest compounded daily is slightly greater, and the interest compounded continuously is a tiny bit greater. The differences are greater if the duration of the loan is longer. For example, if the loan lasts for a year, then the difference between simple and daily compounded interest is several hundred dollars.
The difference between interest compounded daily and continuously is always fairly small because you're already compounding the interest 365 times per year when you compound daily. If you compounded monthly, there would be a larger difference.
Note that this example doesn't deal with years of different lengths. If the loan includes part of a leap year, then the daily interest rate for that part of the loan should be the annual rate divided by 366 instead of 365.
Download the example to experiment with it and to see additional details.
