Dim 猛饥伏p As Long, n As 肢桥Integer, i As Integer
p = 1
n = Val(InputBox("input n", , 5))
For i = 枝携1 To n
p = p * i
Next
Print p
End Sub
Public Function jiecheng(ByVal n As Integer) As DoubleDim jc As Double
jc = 1
敏禅 For i = 1 To n
jc = jc * i
Next
jiecheng = jc
End Function
2、具体用法:首先打开VB6.0,新建一个标准exe工程;
3、在设计界面上添加一个Command控件,Command1的Caption属性设置为"N的阶乘"。
4、双击Command1控件进入代码编辑界面,编辑代码态扮如下:
Public Function jiecheng(ByVal n As Integer) As DoubleDim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next
帆拿灶 jiecheng = jc
End Function
Private Sub Command1_Click()
Dim sum As Double, n As Integer
n = InputBox("计算的N的阶乘,请输入数字N")
sum = jiecheng(n)
MsgBox (sum)
End Sub
5、运行新建的工程,单击按钮,输入要计算的N的阶乘的数字N,得出阶乘结果。
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)