Description

The compounded interest is the amount of money paid as interest on a loan. This example uses some radio buttons to let the user select the frequency by which to perform the calculation.

Compound Interest Formula P = Principal
r = Annual (Interest) Rate
m = Number of Compounding Periods per Year
n = Total Number of Compounding Periods
A = Amount Earned After n periods

Practical LearningPractical Learning: Creating the Application

  1. Start a new Windows Application named CompoundedInterest
  2. Design the form as followed:

    Compound Interest - Form Design

    Control Name Text Additional Properties
    GroupBox Group Box   Loan Setup  
    Label Label   Principal:  
    TextBox TextBox txtPrincipal TextAlign: Right
    Label Label   Interest Rate:  
    TextBox Text Box txtInterestRate TextAlign: Right
    Label Label   %  
    Label Label   Periods:  
    TextBox TextBox txtPeriods TextAlign: Text
    Label Label   years  
    GroupBox Group Box   Compound Frequency  
    RadioButton Radio Button rdoMonthly    
    RadioButton Radio Button rdoQuarterly    
    RadioButton Radio Button rdoSemiannually    
    RadioButton Radio Button rdoAnnually    
    GroupBox Group Box   Calculations Results  
    Label Label   Interest Earned:  
    TextBox Text Box txtInterestEarned 0.00 TextAlign: Right
    Label Label   Amount Earned:  
    TextBox Text Box txtFutureValue 0.00 TextAlign: Right
    Button Button btnCalculate Calculate  
    Button Button btnClose Close  
  3. On the form, double-click the Calculate button and implement its Click() event as follows:
    namespace CompoundedInterest
    {
        public partial class Exercise : Form
        {
            public Exercise()
            {
                InitializeComponent();
            }
    
            private void btnCalculate_Click(object sender, EventArgs e)
            {
                double principal    = 0.00,
                       interestRate = 0.00,
                       periods      = 0.00;
                int    compoundType;
    
                // Retrieve the value of the principal
                try
                {
                    principal = double.Parse(txtPrincipal.Text);
                }
                catch (FormatException fe)
                {
                    MessageBox.Show("You must provide a valid value for the loan amount. " + Environment.NewLine +
                                    "The error produced is: " + fe.Message,
                                    "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                // Retrieve the interest rate
                try
                {
                    interestRate = double.Parse(txtInterestRate.Text) / 100;
                }
                catch (FormatException fe)
                {
                    MessageBox.Show("You must provide a valid value for the interest rate applied on the loan."
                                    + Environment.NewLine +
                                    "The error produced is: " + fe.Message,
                                    "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                // Get the number of years
                try
                {
                    periods = double.Parse(txtPeriods.Text);
                }
                catch (FormatException fe)
                {
                    MessageBox.Show("You must provide a valid number of years for the duration of the loan."
                                    + Environment.NewLine +
                                    "The error produced is: " + fe.Message,
                                    "Compound Interest", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
    
                // Find out what radio button was clicked to apply the compound frequency
                if (rdoMonthly.Checked)
                    compoundType = 12;
                else if (rdoQuarterly.Checked)
                    compoundType = 4;
                else if (rdoSemiannually.Checked)
                    compoundType = 2;
                else
                    compoundType = 1;
    
                // These values will make the calculation easier to read
                double i = interestRate / compoundType;
                double n = compoundType * periods;
    
                // Perform the necessary calculations
                double ratePerPeriod = interestRate / periods;
                double futureValue = principal * Math.Pow(1 + i, n);
                double interestEarned = futureValue - principal;
    
                // Display the values in the appropriate text boxes
                txtInterestEarned.Text = interestEarned.ToString("C");
                txtFutureValue.Text = futureValue.ToString("C");
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }
  4. Return to the form and double-click the Close button
  5. Implement its OnClick() event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  6. To test the application, press Ctrl + F5

    Compound Interest - Results

  7. In the top text boxes, specify the values as follows:
    Principal: 16500
    Interest Rate: 12.75
    Periods:       5
  8. On the form, click the Monthly radio button:

    Compound Interest - Results

  9. Click the Calculate button:

    Compound Interest - Results

  10. Click the Quarterly radio button then click Calculate button:

    Compound Interest - Results

  11. Close the form and return to your programming environment
  12. Close your programming environment

Application


Home Copyright © 2010-2026, FunctionX Friday 27 December 2024, 13:02 Home