Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

p>5. Write a program to analyze a mortgage. The user should enter the amount of

ID: 3622778 • Letter: P

Question

p>5. Write a program to analyze a mortgage. The user should enter the amount of the loan, the annual 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 it is reasonable. If bad data have been supplied, the user should be so ad- vised. Otherwise, the monthly payment and the total amount of interest paid should be displayed. The formula for the monthly payment is<br />payment = p*r/11 - 11 + r2&#191;1-n22,<br />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 percent) and 1 (for 100 percent), and n is the duration of the loan in months. The formula for the total interest paid is<br />totalinterest = n*payment - p.<br />(Test the program for a mortgage of $240,000 at 6% annual rate of interest, and du- ration 360 months. Such a mortgage will have a monthly payment of $1,438.92 and total interest of $278,011.65.)<br />This page intentionally left blank<br />(An Introduction to Programming Using Visual Basic&#174; 2008, 7th Edition. Prentice Hall/CourseSmart, 07/03/2008. 165 - 166). <br />&lt;vbk:PBK9780136060734#page(165)&gt;</p>

Explanation / Answer

Private Sub Command1_Click()

    Dim p As Long

    Dim r As Double

    Dim r1 As Double

    Dim n As Integer

    Dim m As Double

    Dim t As Double

'CHECK ENTRIES

    If IsNumeric(Text1) = False Or Val(Text1) < 1 Then

        MsgBox "Invalid Loan Amount! Try again."

        Text1 = ""

        Text1.SetFocus

        Exit Sub

    End If

    If Right(Text2, 1) = "%" Then Text2 = Left(Text2, Len(Text2) - 1)

    If IsNumeric(Text2) = False Or Val(Text2) > 100 Or Val(Text2) < 0 Then

        MsgBox "Invalid Interest Rate! Try again."

        Text2 = ""

        Text2.SetFocus

        Exit Sub

    End If

    If Text2 >= 1 Then Text2 = Text2 / 100

    If IsNumeric(Text3) = False Or Val(Text3) < 1 Or Int(Val(Text3)) <> Val(Text3) Then

        MsgBox "Invalid Duration! Try again."

        Text3 = ""

        Text3.SetFocus

        Exit Sub

    End If

'CALCULATE

    p = Text1

    r = Text2

    n = Text3

    r1 = r / 12

    If r > 0 Then

        m = p * r1 / (1 - (1 + r1) ^ -n)

    Else

        m = p / n

    End If

    t = n * m - p

'DISPLAY RESULTS

    Text1 = Format(Text1, "$#,###,##0")

    Text2 = Format(Text2, "0.00%")

    Text4 = Format(m, "$#,##0.00")

    Text5 = Format(t, "$#,##0.00")

End Sub

Private Sub Command2_Click()

    Unload Me

End Sub

Private Sub Text1_Click()

    Text1 = ""

End Sub

Private Sub Text2_Click()

    Text2 = ""

End Sub

Private Sub Text3_Click()

    Text3 = ""

End Sub