Home

Example Application: The Compound Interest

 

Compound Interest - Results

Introduction

The application we are going to create is used to calculate the amount owed on a loan using the principal, the interest rate, and the period.

Windows Controls:

     

Practical Learning Practical Learning: Introducing Radio Buttons

The application we are going to create is used to calculate the amount owed on a loan using the following formula:

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
  1. Start a new Windows Application named CompoundInterest1
  2. In the Solution Explorer, right-click Form1.vb and click Rename
  3. Type CompoundInterest.vb and press Enter
  4. Design the form as followed:
     
    Compound Interest - Form Design
    Control Name Text Additional Properties
    GroupBox GroupBox   Loan Setup  
    Label Label   Principal:  
    TextBox TextBox txtPrincipal 0.00 TextAlign: Right
    Label Label   Interest Rate:  
    TextBox TextBox txtInterestRate 8.25 TextAlign: Right
    Label Label   %  
    Label Label   Number of Periods:  
    TextBox TextBox txtPeriods 1 TextAlign: Text
    Label Label   years  
    GroupBox GroupBox   Compound Frequency  
    RadioButton RadioButton rdoMonthly    
    RadioButton RadioButton rdoQuarterly    
    RadioButton RadioButton rdoSemiannually    
    RadioButton RadioButton rdoAnnually    
    GroupBox GroupBox   Results  
    Label Label   Interest Earned:  
    TextBox TextBox txtInterestEarned 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   Amount Earned:  
    TextBox TextBox txtFutureValue 0.00 TextAlign: Right
    ReadOnly: True
    Button Button btnCalculate Calculate  
    Button Button btnClose Close  
  5. On the form, double-click the Close button and implement the event as follows:
     
    Private Sub btnCloseClick(ByVal sender As Object, 
                                   ByVal e As System.EventArgs) 
                                   Handles btnClose.Click
            End
    End Sub
  6. On the form, click Monthly
  7. In the Properties window, double-click Checked to set its value to True
  8. On the form, double-click the Calculate button and implement its Click() event as follows:
     
    Private Sub btnCalculateClick(ByVal sender As System.Object, 
                                       ByVal e As System.EventArgs) 
                                       Handles btnCalculate.Click
            Dim Principal As Double
            Dim InterestRate As Double
            Dim InterestEarned As Double
            Dim FutureValue As Double
            Dim RatePerPeriod As Double
            Dim Periods As Double
            Dim CompoundType As Integer
    
            ' Retrieve the value of the principal
            Try
                Principal = CDbl(txtPrincipal.Text)
            Catch ex As Exception
                MsgBox("The value you entered for the principal " & 
                       "is not valid" & vbCrLf & "Please try again")
            End Try
    
            ' Retrieve the interest rate
            Try
                InterestRate = CDbl(txtInterestRate.Text) / 100
            Catch ex As Exception
                MsgBox("The value you entered for the interest rate " & 
                       "is not valid" & vbCrLf & "Please try again")
            End Try
    
            ' 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
    
            ' Get the number of periods
            Try
                Periods = CDbl(txtPeriods.Text)
            Catch ex As Exception
                MsgBox("The value you entered for the number of periods " & 
                       "is not valid" & vbCrLf & "Please try again")
            End Try
    
            ' These values will make the calculation easier to read
            Dim i As Double = InterestRate / CompoundType
            Dim n As Double = CompoundType * Periods
    
            ' Perform the necessary calculations
            RatePerPeriod = InterestRate / Periods
            FutureValue = Principal * Math.Pow(1 + i, n)
            interestEarned = FutureValue - Principal
    
            ' Display the values in the appropriate text boxes
            txtInterestEarned.Text = FormatCurrency(InterestEarned)
            txtFutureValue.Text = FormatCurrency(FutureValue)
    End Sub
  9. Test the application
     
    Compound Interest - Results
  10. After using it, close the form and return to your programming environment

Compound Interest

 

Home Copyright © 2008-2012 FunctionX