vbi-49

(i) Open a new project and change the forms caption property from forms to factorial of a number.

(ii) Drag three label, two text boxes and three command button to on the form and change its property into the following format.

Object / Control

Property

Value

Label1 Caption

Font

Alignment

Border Style

“Factorial of a Number”

Ms Sans Serif, 14, Bold

2 – Center

1 – Fixed

Label2 Caption

Font

Alignment

Border Style

“N = ”

Ms Sans Serif, 12, Bold

2 – Center

1 – Fixed

Label3 Caption

Font

Alignment

Border Style

“N! = ”

Ms Sans Serif, 12, Bold

2 – Center

1 – Fixed

Text1 Text

Font

(None)

Ms Sans Serif, 12, Bold

Text2 Text

Font

(None)

Ms Sans Serif, 12, Bold

Command1 Caption

Font

“Go”

Ms Sans Serif, 14, Bold

Command2 Caption

Font

“Clear”

Ms Sans serif, 14, Bold

Command3 Caption

Font

“Exit”

Ms Sans Serif, 12, Bold

 

(iii) After design form, double click on the form to bring the code template. Write the function procedure under the general section. Select tools menu then select add procedure and in the dialog box, click the function option button and enter its name factorial and write code in the code window in following format –

Function Factorial(n As Integer) As Long
    Dim i As Integer
    If n < 1 Then
        Beep
        MsgBox ("ERROR - Please Try Again")
    Else
        Factorial = 1
        For i = 1 To n
            Factorial = Factorial * i
        Next i
    End If
End Function

(iv) Double click on the Go command button and enter the code as follow –

Private Sub Command1_Click()
    Dim n As Integer, nfact As Long
    n = Val(Text1.Text)
    nfact = Factorial(n)
    Text2.Text = Str(nfact)
End Sub

(v) Double click the Clear command button and enter the code as follows –

Private Sub Command2_Click()
    Text1.Text = ""
    Text2.Text = ""
End Sub

(vi) Double click the Exit command button and enter the code as follows –

Private Sub Command3_Click()
    End
End Sub

vbo-49

Advertisements