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 Microsoft Visual Studio
  2. On the Visual Studio 2026 dialog box, click Create a New Project
  3. In the Create a New Project dialog box, in the Languages combo box, select Visual Basic
  4. In the list project templates, click Windows Forms App
  5. Click Next
  6. In the Configure Your New Project dialog box, set the Project Name to CompoundedInterest
  7. Click Next
  8. In the Additional Information dialog box, in the Framework combo box, select the highest version (.NET 10.0 (Long Term Support)).
    Click Create
  9. In the Solution Explorer, right-click Form1.cs and click Rename
  10. Type Exercise (to get Exercise.cs) and press Enter twice
  11. Design the form as followed:

    Compound Interest - Form Design

    Control Name Text Additional Properties
    GroupBox GroupBox   Loan Setup  
    Label Label   Principal:  
    TextBox TextBox TxtPrincipal TextAlign: Right
    Label Label   Interest Rate:  
    TextBox TextBox TxtInterestRate TextAlign: Right
    Label Label   %  
    Label Label   Periods:  
    TextBox TextBox TxtPeriods TextAlign: Text
    Label Label   years  
    GroupBox GroupBox   Compound Frequency  
    RadioButton RadioButton rdoMonthly    
    RadioButton RadioButton rdoQuarterly    
    RadioButton RadioButton rdoSemiannually    
    RadioButton RadioButton rdoAnnually    
    GroupBox GroupBox   Calculations Results  
    Label Label   Interest Earned:  
    TextBox TextBox TxtInterestEarned 0.00 TextAlign: Right
    Label Label   Amount Earned:  
    TextBox TextBox TxtFutureValue 0.00 TextAlign: Right
    Button Button btnCalculate Calculate  
    Button Button btnClose Close  
  12. On the form, double-click the Calculate button and implement its Click() event as follows:
    Public Class Exercise
        Private Sub BtnCalculateClick(sender As Object, e As EventArgs) Handles BtnCalculate.Click
            Dim Principal = 0.00
            Dim interestRate = 0.00
            Dim periods = 0.00
            Dim compoundType
    
            REM Retrieve the value of the principal
            Try
    
                Principal = CDbl(TxtPrincipal.Text)
            Catch fe As FormatException
                MsgBox("You must provide a valid value for the loan amount. " +
                       Environment.NewLine +
                       "The error produced is: " + fe.Message,
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
                       "Compound Interest")
            End Try
    
            REM Retrieve the interest rate
            Try
                interestRate = CDbl(TxtInterestRate.Text) / 100
            Catch fe As FormatException
                MsgBox("You must provide a valid value for the interest rate applied on the loan." +
                       Environment.NewLine + "The error produced is: " + fe.Message,
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
                                    "Compound Interest")
            End Try
    
            REM Get the number of years
            Try
                periods = CDbl(TxtPeriods.Text)
            Catch fe As FormatException
                MsgBox("You must provide a valid number of years for the duration of the loan." +
                       Environment.NewLine +
                       "The error produced is: " + fe.Message,
                       MsgBoxStyle.OkOnly Or MsgBoxStyle.Information,
                                    "Compound Interest")
            End Try
    
            REM Find out what radio button was clicked to apply the compound frequency
            If RdoMonthly.Checked Then
                compoundType = 12
            ElseIf (RdoQuarterly.Checked) Then
                compoundType = 4
            ElseIf (RdoSemiannually.Checked) Then
                compoundType = 2
            Else
                compoundType = 1
            End If
    
            REM These values will make the calculation easier to read
            Dim i = interestRate / compoundType
            Dim n = compoundType * periods
    
            REM Perform the necessary calculations
            Dim ratePerPeriod = interestRate / periods
            Dim futureValue = Principal * Math.Pow(1 + i, n)
            Dim interestEarned = futureValue - Principal
    
            REM Display the values in the appropriate text boxes
            TxtInterestEarned.Text = Format(interestEarned, "STANDARD")
            TxtFutureValue.Text = Format(futureValue, "STANDARD")
        End Sub
    End Class
  13. Return to the form and double-click the Close button
  14. Implement its OnClick() event as follows:
    Private Sub BtnCloseClick(sender As Object, e As EventArgs) Handles BtnClose.Click
        End
    End Sub
  15. To test the application, press Ctrl + F5

    Compound Interest - Results

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

    Compound Interest - Results

  18. Click the Calculate button:

    Compound Interest - Results

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

    Compound Interest - Results

  20. Close the form and return to your programming environment
  21. Close your programming environment

Application


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