Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Car Loan

Question from Mike:

Need help working a code called car loan. The book is the Introduction to Programming Using Visual Basic 2012. Programming project no. 3, p. 162. I think my math is totally wrong.

Description:

Write a program to analyze a car loan. See figure below. The user should enter the amount of the loan, the annual percentage rate of interest, and the duration of the loan in months. When the user clicks on the button, the information that was entered should be checked to make sure that it is reasonable. If bad data has been supplied, the user should be so advised. Otherwise, the monthly payment and the total amount of interest paid should be displayed. The formula for the monthly payment is

Car Loan Equation 1

where p is the amount of the loan, r is the monthly interest rate (annual rate divided by 12) given as a number between 0 (for 0%) and 1 (for 100%), and n is the duration of the loan in months. The formula for the total interest paid is

Car Loan Equation 2Here is an example of what the user interface might look like:

Car Loan Screenshot

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmCarLoan  Car Loan Form Holds Controls
 txtLoanAmt  N/A  TextBox  Captures loan amount
 txtIntRate  N/A  TextBox  Captures interest rate
 txtNumOfMonths  N/A  TextBox  Captures number of months
 btnCalc  Calculate  Button  Calculates and outputs results
 txtMonthlyPmt  N/A  TextBox  Displays monthly payment
 txtTotalInt  N/A  TextBox  Displays total interest

Hints:

  • The equations are probably the trickiest part of this program.
  • Since the second equation above is dependent upon the answer from the first equation above, the order you place them in the program is important.
  • Equation 1: payment = (loanAmt * (intRate / 12)) / (1 – (1 + (intRate / 12)) ^ (-months))
  • Equation 2: totalInt = months * payment – loanAmt

Write the Code:

Public Class frmCarLoan
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
‘ Declare local variables
Dim loanAmt As Double = 0
Dim intRate As Double = 0
Dim months As Integer = 0
Dim payment As Double = 0
Dim totalInt As Double = 0

‘ Collect data from user and store in variables
If (CDbl(txtLoanAmt.Text) < 0) Then
MessageBox.Show(“Loan Amount can’t be a negative number.”)
txtLoanAmt.Clear()
txtLoanAmt.Focus()
Else
loanAmt = CDbl(txtLoanAmt.Text)
End If

If (CDbl(txtIntRate.Text) < 0) Then
MessageBox.Show(“Interest Rate can’t be a negative number.”)
txtIntRate.Clear()
txtIntRate.Focus()
ElseIf (CDbl(txtIntRate.Text) > 50) Then
MessageBox.Show(“You should probably shop around for a better interest rate.”)
txtIntRate.Clear()
txtIntRate.Focus()
Else
intRate = CDbl(txtIntRate.Text) / 100
End If

If (CDbl(txtNumOfMonths.Text) < 0) Or (CDbl(txtNumOfMonths.Text) > 100) Then
MessageBox.Show(“Loan duration should be between 1 and 100 months.”)
txtNumOfMonths.Clear()
txtNumOfMonths.Focus()
Else
months = CInt(txtNumOfMonths.Text)
End If

‘ Perform calculations
payment = (loanAmt * (intRate / 12)) / (1 – (1 + (intRate / 12)) ^ (-months))
totalInt = months * payment – loanAmt

‘ Output results
txtMonthlyPmt.Clear()
txtTotalInt.Clear()
If (loanAmt > 0) And (intRate > 0) And (months > 0) Then
txtMonthlyPmt.Text = payment.ToString(“C”)
txtTotalInt.Text = totalInt.ToString(“C”)
End If
End Sub
End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2025 · Metro Pro Theme on Genesis Framework · WordPress · Log in