Calculate credit payments in C#


For each month the program:

  • Calculates the payment first. This is either a percent of the balance or the minimum amount, whichever is greater.
  • Calculates interest after the payment is calculated but before it is subtracted from the balance.
  • Adds the interest to the balance.
  • If the payment is greater than the balance, the payment is set equal to the balance.
  • Subtracts the payment from the balance.
  • Displays the results for the month.

This method agrees well with the calculator at

The following code does all the work.

// Calculate the payments.
private void btnGo_Click(object sender, EventArgs e)
    // Get the parameters.
    decimal balance =
        decimal.Parse(txtInitialBalance.Text, NumberStyles.Any);
    decimal interest_rate =
        decimal.Parse(txtInterestRate.Text.Replace("%", "")) / 100;
    decimal payment_percent =
        decimal.Parse(txtPaymentPercent.Text.Replace("%", "")) / 100;
    decimal min_payment =
        decimal.Parse(txtMinPayment.Text, NumberStyles.Any);
    interest_rate /= 12;

    decimal total_payments = 0;

    // Display the initial balance.
    ListViewItem new_item = lvwPayments.Items.Add("0");

    // Loop until balance == 0.
    for (int i = 1; balance > 0; i++)
        // Calculate the payment.
        decimal payment = balance * payment_percent;
        if (payment < min_payment) payment = min_payment;

        // Calculate interest.
        decimal interest = balance * interest_rate;
        balance += interest;

        // See if we can pay off the balance.
        if (payment > balance) payment = balance;
        total_payments += payment;
        balance -= payment;

        // Display results.
        new_item = lvwPayments.Items.Add(i.ToString());

    // Display the total payments.
    txtTotalPayments.Text = total_payments.ToString("c");

To model a fixed payment (for example, $200 per month), set Payment % to 0 and set Min Payment to the monthly amount you want to pay.

Note that if you don’t rack up this big debt and you put the payments in a savings account or some other investment, then compound interest will work in your favor instead of against you and you’ll make money instead of paying out a lot of interest. For an example that calculates compound interest with a monthly investment, see Calculate the value of a monthly investment in C#.

Suppose you want to buy something for $5,000.00. Using this example and the default settings, you end up paying $8,109.24 over 138 months. That’s an average payment of $58.76, although in fact you pay a lot more in the beginning because your minimum payment is larger when the balance is bigger.

Instead of putting the $5,000.00 on your credit card, suppose you put $60.00 per month in an account that pays 7.00% interest. After only 69 months you would have saved $5,079.20. You would have enough for the $5,000.00 purchase with a little left over in half the time it would have taken you to pay off the credit card debt and with smaller payments initially!

Of course, there are a couple of catches. First, can you find an investment that pays 7.00% these days? If you only get a 1.00% return, you still raise $5,025.61 in 81 months. That’s not as fast but it’s still a lot faster than paying off the credit card debt and with smaller contributions.

The moral is that you can save a lot of money if you save up for something instead of buying it on credit. You’ll save the interest payments and be finished saving sooner than you would be finished paying off credit card debt.

The brief article How to Get Rid of Credit Card Debt in 2018 on the lendedu web site gives three not completely obvious tips for paying off debt and improving your credit score.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, calculations, finance, mathematics and tagged , , , , , , , , , , , , , . Bookmark the permalink.

11 Responses to Calculate credit payments in C#

  1. Les says:

    It will always pass on 1st go making these calculation not working correctly.
    if (payment < balance) payment = balance;

  2. Jason says:

    Hi, this works really well, though could you supply the above code in WPF C# format??

  3. Jason says:

    ok thanks for your help, it was just this part in converting it over to a datagrid:

    ListViewItem new_item = lvwPayments.Items.Add(“0”);

    but I think I have the answer.

  4. Pingback: Calculate credit payments in WPF and C# - C# HelperC# Helper

  5. Kim says:

    What would be the code to get the table? I am not very good with coding. I need the places for the person to enter data. Could you help me with that?

    • RodStephens says:

      If you download the program and run it, you can enter the values on the left (initial balance, interest rate, payment %, and min payment) and then click Go to make the program generate the values.

      If you look at the post’s code, the lines after // Display results. are the ones that add the values to the table. You could save those values into a text file or something if you wanted to.

  6. Abdullah says:

    what if I want to add an annual fee to the calculation

Comments are closed.