# Need help with a Visual Basic code?

If anyone can just help me with the code for the following situation, i would greatly appreciate it:

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 command 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 advised. Otherwise, the monthly payment and the total

amount of interest paid should be displayed. The formula for the monthly payment

is

payment = p * r / (1 – (1 + r) ^ (–n))

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. The formula for the total interest paid is

total interest = n * payment – p

(Test the program for a mortgage of $ 240,000 at 6% annual rate of interest, and

duration 360 months. Such a mortgage will have a monthy payment of

$ 1,438.92 and total interest of $ 278,011.65)

thank you!!!!

please can anyone help me?

Sure. First you’ll need three textboxes on the form, called “txtLoan”, “txtRate”, and “txtMonths”.

I also found that the

Then in the sub for the commandbutton_click event handler:

Dim n As Double, r As Double, p As Double

Try

n = CType(txtLoan.Text, Double)

Catch ex As Exception

MessageBox.Show(“Parsing failed, please use a number value for the Months”, “Parsing Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)

Return

End Try

Try

r = CType(txtRate.Text, Double)

Catch ex As Exception

MessageBox.Show(“Parsing failed, please use a number value for the Rate”, “Parsing Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)

Return

End Try

Try

p = CType(txtLoan.Text, Double)

Catch ex As Exception

MessageBox.Show(“Parsing failed, please use a number value for the Loan”, “Parsing Error”, MessageBoxButtons.OK, MessageBoxIcon.Error)

Return

End Try

If (r < 0 Or r > 100) Then

MessageBox.Show(“Please enter a percentage between 0 and 100 for the rate value.”, “Bad Rate Value”, MessageBoxButtons.OK, MessageBoxIcon.Error)

Return

End If

If (n < 0) Then MessageBox.Show("Please enter a positive value for the time (months).", "Bad Months Value", MessageBoxButtons.OK, MessageBoxIcon.Error) Return End If n = n / 12 r = r / 100 Dim payment As Double = p * r / (1 - (1 + r) ^ (-n)) Dim interest As Double = n * payment - p MessageBox.Show("interest = $ " + (interest).ToString() & vbCrLf & "payment = $ " + (payment).ToString()) I get slightly different results from the ones you've quoted, but you can see I've used the correct formulas, feel free to tweak them a bit. Hope it helps 🙂 - D.E.