The most common types of loans are mortgage loans, which are used to finance the purchase of a house, and car loans. A loan consists of four components – amount, interest rate, duration, and periodic payment. The purpose of this programming project is to calculate the value of any one of the components given the values of the other three components.
We will assume that the duration is in months, that interest (given as a percent) is compounded monthly, and that payments are made monthly. Currently home mortgages typically have an interest rate of about 4% and a duration of 30 years (360 months). Car loans typically have an interest rate of about 6% and a duration of between 3 and 5 years (36 to 60 months).
Four built-in Visual Basic financial functions that perform loan calculations are as follows:
Pmt(interest rate / 1200, duration, -amount) gives the monthly payment
1200 * Rate(duration, monthly payment, -amount) gives the stated interest rate
PV(interest rate / 1200, duration, -monthly payment) gives the amount of the loan
NPer(interest rate / 1200, monthly payment, -amount) gives the duration of the loan in months
Write a program in which the user specifies a value to calculate, enters three other values, and clicks on the Calculate Value button. See the figure below for a possible outcome. Before any calculations are made, the program should use a function to validate that a radio button is checked and that the values entered into the three text boxes corresponding to unchecked radio buttons are valid.

Hints:
When you are running the program, you should only check the radio button for the component that you are seeking to compute. For example, if you want to calculate the monthly payment, the other three buttons must remain unchecked. See the example above.
The equations above are built-in visual basic functions and must be used exactly as they are written above.
For the sake of simplicity, when you are entering the three pieces of information that you already have, be sure to enter them without punctuation. For example, the amount of the loan should be entered as “14500” and not “$14,500”. This will simplify the input validation process and prevent needing to code complex exception handlers, which we will learn about later.
Suggested Control Names and Attributes:
| Name Property | Text Property | Control Type | Notes |
| frmLoan | Loan Calculator | Form | Holds Controls |
| radAmount | Amount of Loan | Radio Button | Calculates amount of loan when selected. |
| radRate | Interest Rate | Radio Button | Calculates the interest rate when selected. |
| radDuration | Duration of Loan | Radio Button | Calculates the duration of loan when selected. |
| radPayment | Monthly Payment | Radio Button | Calculates the monthly payment when selected. |
| btnCalculate | Calculate Value | Button | When triggered, relevant computation is performed. |
| btnClear | Clear All Values | Button | When triggered, clears all values on the form. |
Write the Code:
' Project: Loan Calculator
' Description: Simulates a loan calculator. Given three out of four values, program calculates the amount, interest rate
' duration of loan or monthly payment.
Public Class frmLoan
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
' Clears values of the text boxes
txtAmount.Text = ""
txtRate.Text = ""
txtDuration.Text = ""
txtPayment.Text = ""
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
' Evaluates input and displays calculations or error message
If InputIsValid() = True Then
CalculateValue()
Else
MessageBox.Show("You forgot to enter data in at least one box")
End If
End Sub
Function InputIsValid() As Boolean
' Make sure input is valid
' Declare local variable
Dim blank As Boolean = True ' variable to make sure there is valid input in unchecked text boxes
' Check to see if unchecked buttons have blank text boxes
If (radAmount.Checked = True) Then
blank = False
ElseIf txtAmount.Text = "" Then
blank = True
End If
If (radDuration.Checked = True) Then
blank = False
ElseIf txtDuration.Text = "" Then
blank = True
End If
If (radPayment.Checked = True) Then
blank = False
ElseIf txtPayment.Text = "" Then
blank = True
End If
If (radRate.Checked = True) Then
blank = False
ElseIf txtRate.Text = "" Then
blank = True
End If
Return (blank = False)
End Function
Sub CalculateValue()
' calculate missing number
If radAmount.Checked = True Then
txtAmount.Text = PV(CDbl(txtRate.Text) / 1200, CDbl(txtDuration.Text), -CDbl(txtPayment.Text)).ToString("C")
End If
If radRate.Checked = True Then
txtRate.Text = (1200 * Rate(CDbl(txtDuration.Text), CDbl(txtPayment.Text), -CDbl(txtAmount.Text))).ToString("N1")
End If
If radDuration.Checked = True Then
txtDuration.Text = NPer(CDbl(txtRate.Text) / 1200, CDbl(txtPayment.Text), -CDbl(txtAmount.Text)).ToString("N0")
End If
If radPayment.Checked = True Then
txtPayment.Text = Pmt(CDbl(txtRate.Text) / 1200, CDbl(txtDuration.Text), -CDbl(txtAmount.Text)).ToString("C")
End If
End Sub
End Class