1、For语句实现
Private Sub Command1_Click()
Dim s As Long, n As Integer, i As Integer
n = Val(Text1.Text)
s = 1
For i = 1 To n
s = s * i
Next i
Label4.Caption = Str(s)
End Sub
2、Do While语句实现
Private Sub Command1_Click()
Dim s As Long, n As Integer, i As Integer
n = Val(Text1.Text)
s = 1
i = 1
Do While i <= n
s = s * i
i = i + 1
Loop
Label4.Caption = Str(s)
End Sub
扩展资料:
1~10的阶乘的结果如下:
1!=1
2!=2*1=2
3!=3*2*1=6
4!=4*3*2*1=24
5!=5*4*3*2*1=120
6!=6*5*4*3*2*1=720
7!=7*6*5*4*3*2*1=5040
8!=8*7*6*5*4*3*2*1=40320
9!=9*8*7*6*5*4*3*2*1=362880
10!=10*9*8*7*6*5*4*3*2*1=3628800
在VB中求阶乘的部分代码:
Private Function fact(m As Integer) As Long
Dim i As Integer
fact = 1
For i = 1 To m
fact = fact * i
Next i
End Function
Private Sub Command1_Click()
Dim n As Integer, sum As Long
Dim i As Integer
n = Val(Text1.Text)
For i = 1 To
扩展资料:
1~10的阶乘的结果如下:
1!=1
2!=2*1=2
3!=3*2*1=6
4!=4*3*2*1=24
5!=5*4*3*2*1=120
6!=6*5*4*3*2*1=720
7!=7*6*5*4*3*2*1=5040
8!=8*7*6*5*4*3*2*1=40320
9!=9*8*7*6*5*4*3*2*1=362880
10!=10*9*8*7*6*5*4*3*2*1=3628800
你给的代码和阶乘有什么关系?
Private Sub Command1_Click()
Dim x As Long, i As Long, y As Double
x = InputBox("请输入要计算阶乘的数字:")
y = 1
For i = 1 To x
y = y * i
Next
Print y
End Sub
用递归函数和输入框(输入数字)和按钮(OK确定)和一个字符label(输出结果)
Private
Sub
Form_Click()
Dim
n
As
Integer,
t#,
i%
n
=
InputBox("输入正整数",
"阶乘")
t
=
1
i
=
1
Do
While
i
<=
n
t
=
t
*
i
i
=
i
+
1
Loop
Print
n
&
"!="
&
t
End
Sub