19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Private Sub cmdCompute_Click()<br />

Dim p As Single, r As Single, c As Single, n As Single<br />

‘Find the future value of a bank deposit<br />

Call InputData(p, r, c, n)<br />

Call DisplayBalance(p, r, c, n)<br />

End Sub<br />

Private Sub DisplayBalance(p As Single,r As Single,c As Single,n As Single)<br />

Dim balance As Single<br />

‘Display the balance in the picture box<br />

picBalance.Cls<br />

balance = FV(p, r, c, n)<br />

picBalance.Print FormatCurrency(balance)<br />

End Sub<br />

Private Function FV(p As Single,r As Single,c As Single,n As Single) As Single<br />

Dim i As Single, m As Single<br />

‘Find the future value of a bank savings account<br />

‘p principal, the amount deposited<br />

‘r annual rate of interest<br />

‘c number of times interest is compounded per year<br />

‘n number of years<br />

‘i interest rate per period<br />

‘m total number of times interest is compounded<br />

i = r / c<br />

m = c * n<br />

FV = p * ((1 + i) ^ m)<br />

End Function<br />

Private Sub InputData(p As Single, r As Single, c As Single, n As Single)<br />

‘Get the four values from the text boxes<br />

p = Val(txtAmount.Text)<br />

r = Val(txtRate.Text)<br />

c = Val(txtNumComp.Text)<br />

n = Val(txtNumYrs.Text)<br />

End Sub<br />

[Run, type 100, .04, 4, <strong>and</strong> 5 into the text boxes, then click the comm<strong>and</strong> button.]<br />

EXAMPLE 5<br />

Some computer languages have a useful built-in function called Ceil that is similar to the function Int,<br />

except that it rounds noninteger numbers up to the next integer. For instance, Ceil(3.2) is 4 <strong>and</strong> Ceil(–1.6)<br />

is –1. The following program creates Ceil in <strong>Visual</strong> <strong>Basic</strong> as a user-defined function.<br />

Function Procedures 91

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!