Private Sub Command1_Click()
Dim XX As Double, YY As Integer
XX = 1
YY = Val(InputBox("请输入N的值", "输入", 0))
If YY > 170 Then
MsgBox "你输入的值太大,最大只能计算170的阶乘值!"
YY = 170
End If
For I = 1 To YY
XX = XX * I
Next I
Text1.Text = YY & " 的阶乘 = " & CStr(XX)
End Sub
Private Sub Command1_Click()
Dim i As Integer, n As Integer, s As Double
s = 1
n = Val(Text1.Text)
If n <= 1 Then
Text2.Text = "1"
Exit Sub
Else
For i = 1 To n
s = s * i
Next i
Text2.Text = Format(s, "0")
End If
End Sub
Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
Command1.Caption = "计算"
End Sub
‘调用示例
Private Sub Form_Load()
MsgBox fun_JC(3)
End Sub
'阶乘函数
Private Function fun_JC(ByVal N As Integer) As Long
Dim i As Integer
Dim l As Long
l = 1
If N <= 1 Then
fun_JC = 1
Exit Function
End If
For i = N To 2 Step -1
l = l * i
Next
fun_JC = l
End Function