vb中怎么写6的阶乘代码

1.怎么写vb的阶乘代码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
2.阶乘在VB 中怎么写呀建一个Command1 。中间那部分就是运算程序 。
代码如下 。
=============
Private Sub Command1_Click()
Dim a As Integer, i As Integer, s As Double
s = 1
a = InputBox("输入数")
【vb中怎么写6的阶乘代码】For i = 2 To a
s = s * i
Next
MsgBox a &; "的阶乘是:" & s
End Sub
3.vb编写一个关于阶乘的程序,刚给你写的 希望对你有帮助 给个满意回答吧
Private Sub Command1_Click()
Dim dat1 As Variant
Dim sum As Variant
Dim i As Integer
dat1 = Val(Text1.Text)
sum = 1
If dat1 <= 0 Then
MsgBox "请重新输入",vbExclamation + vbOKOnly,"提示"
Else
For i = 1 To dat1
sum = sum * i
Next i
Text2.Text = CStr(sum)
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Form_Load()
Label1.Caption = "输入数据"
Label2.Caption = "阶乘"
Command1.Caption = "计算"
Command2.Caption = "退出"
Text1.Text = ""
Text2.Text = ""
End Sub
4.visual basic 中阶乘怎么表示在VB中没有直接的运算符或是函数能算一个正整数的阶乘 。在这里需要你自己定义一个函数来算阶乘:
Function TimeStep(Numer As Integer) As Long
TimeStep = 1
For i = 1 To Number
TimeStep = TimeStep * i
Next
End Function
把上述代码复制到这个窗体中所有代码的最上面,即通用(声明)的位置 。此后,在代码中你就可以像用其他正常的函数一样用TimeStep()函数来算阶乘了 。
5.vb如何输入阶乘自己编写函数代码:
Private Function 求阶乘(Byval n As Integer) As Long
Dim i As Integer
Dim Fac As Long
Fac = 1
For i = 1 To n
Fac = Fac * i
Next i
求阶乘 = Fac
End Function
在你需要求阶乘的地方,调用该函数:
例如:
Dim y As Long
y = 求阶乘(10) '求阶乘(10)将用10调用上面的自定义函数,返回10的阶乘
6.vb利用子过程求1Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load dim n as Integer n=textboxnum.text dim i as integer dim sum as integer=0 for i =1 to n sum =sum+Factorial(i) next for textboxfactorial.text=sum End Sub Protected Sub Factorial(ByVal n As Integer) As Integer dim i as integer dim xxx as integer=1 for i =2 to n xxx=xxx*i next for return xxx End Sub手头没有VB6.0,不过大致就是这样吧 。

vb中怎么写6的阶乘代码

文章插图