To find factorial of a number we multiply the terms from 1 to any number we want to find the factorial.
Example we want to find factorial of 4. Then we multiply 1*2*3*4 = 12. So we can do this with VB6 code. First we set an initial value to start from. Then we run a loop for the nth term (of which we have to find the factorial.) we multiply the i of the loop with the previous result. That is stored in a separate variable. That value is updated in each step of the loop. At last when all the terms have been multiplied to it, it returns us the factorial value. See the code below.
Example we want to find factorial of 4. Then we multiply 1*2*3*4 = 12. So we can do this with VB6 code. First we set an initial value to start from. Then we run a loop for the nth term (of which we have to find the factorial.) we multiply the i of the loop with the previous result. That is stored in a separate variable. That value is updated in each step of the loop. At last when all the terms have been multiplied to it, it returns us the factorial value. See the code below.
Private Sub cmdFind_Click()
Dim nVal As Long
Dim gVal As Long
Dim i As Integer
txtNo = Trim$(txtNo)
If Len(txtNo) = 0 Then
MsgBox "Enter number", vbCritical, "Input error"
txtNo.SetFocus
Exit Sub
Else
If Not IsNumeric(txtNo) Then
MsgBox "Only numeric input allowed", vbCritical, "Input error"
txtNo.SetFocus
Exit Sub
End If
End If
nVal = 1
gVal = CLng(txtNo.Text)
For i = 2 To gVal
nVal = nVal * i
Next i
MsgBox "The Factorial for " & CStr(gVal) & " is " & CStr(nVal)
End Sub
Dim nVal As Long
Dim gVal As Long
Dim i As Integer
txtNo = Trim$(txtNo)
If Len(txtNo) = 0 Then
MsgBox "Enter number", vbCritical, "Input error"
txtNo.SetFocus
Exit Sub
Else
If Not IsNumeric(txtNo) Then
MsgBox "Only numeric input allowed", vbCritical, "Input error"
txtNo.SetFocus
Exit Sub
End If
End If
nVal = 1
gVal = CLng(txtNo.Text)
For i = 2 To gVal
nVal = nVal * i
Next i
MsgBox "The Factorial for " & CStr(gVal) & " is " & CStr(nVal)
End Sub